Force expansion of counter value in command argument

In the following MWE I would like to expand both \envnum as well as \itemnum while passing them to \cmdwithexpandedargs but only the first one got expanded. Note the different output between \wlog and \printcmd, this still intrigues me.

\documentclass{letter}  \newcounter{nenv} \newcommand\printcmd{}  \makeatletter \newcommand\cmdwithexpandedargs[1]{%     \[email protected]@macro\printcmd{#1 - } } \makeatother  \newenvironment{test}{%     \stepcounter{nenv}%     \let\olditem\item     \renewcommand\item{%         \olditem         \xdef\envnum{\arabic{nenv}}%         \xdef\itemnum{\alph{enumi}}%         \wlog{\envnum.\itemnum}         \expandafter\cmdwithexpandedargs\expandafter{\envnum\itemnum}%     }%     \begin{enumerate}% }{\end{enumerate}}  \begin{document} \begin{test}   \item one   \item two  \end{test} \begin{test}   \item one   \item two   \item three \end{test} \printcmd \end{document} 

The \wlog output "1.a - 1.b - 2.a - 2.b - 2.c" is what I want, but it is not what \printcmd gives: "1c - 1c - 2c - 2c - 2c". I'm accepting LaTeX3 (expl3) solutions here, if required!

Replay

The difference is because you aren't expanding \itemnum when passing it to \cmdwithexpandedargs, but \wlog fully expands its argument.

You can do it more efficiently:

\documentclass{letter}

\newcounter{nenv}
\newcommand\printcmd{}

\makeatletter
\newenvironment{test}{%
    \stepcounter{nenv}%
    \let\olditem\item
    \renewcommand\item{%
        \olditem
        \xdef\printcmd{\printcmd\arabic{nenv}\alph{enumi} - }%
        \wlog{\arabic{nenv}.\alph{enumi}}%
    }%
    \begin{enumerate}%
}{\end{enumerate}}
\makeatother

\begin{document}
\begin{test}
  \item one
  \item two
\end{test}
\begin{test}
  \item one
  \item two
  \item three
\end{test}
\printcmd
\end{document}

No need to do separate \xdef (global is actually useless and could simply be \edef).

Force expansion of counter value in command argument

In the log file you find

1.a
1.b
2.a
2.b
2.c

You need to expand not only \envnum, but also \itemnum. That means the one line got revised to \expandafter\expandafter\expandafter\cmdwithexpandedargs\expandafter\expandafter\expandafter{\expandafter\envnum\itemnum}. After the first round of expansion, \itemnum is expanded leaving what you started with, \expandafter\cmdwithexpandedargs\expandafter{\envnum\itemnum}. This second round expands \envnum and you are good to go.

\documentclass{letter}

\newcounter{nenv}
\newcommand\printcmd{}

\makeatletter
\newcommand\cmdwithexpandedargs[1]{%
    \[email protected]@macro\printcmd{#1 - }
}
\makeatother

\newenvironment{test}{%
    \stepcounter{nenv}%
    \let\olditem\item
    \renewcommand\item{%
        \olditem
        \xdef\envnum{\arabic{nenv}}%
        \xdef\itemnum{\alph{enumi}}%
        \wlog{\envnum.\itemnum}
        \expandafter\expandafter\expandafter\cmdwithexpandedargs\expandafter\expandafter\expandafter{\expandafter\envnum\itemnum}%
    }%
    \begin{enumerate}%
}{\end{enumerate}}

\begin{document}
\begin{test}
  \item one
  \item two
\end{test}
\begin{test}
  \item one
  \item two
  \item three
\end{test}
\printcmd
\end{document}

Force expansion of counter value in command argument



AFTERNOTE

I got some magic code from Christian Tellechea:

% CODE FROM CHRISTIAN TELLECHEA
\makeatletter
\def\expandtimes(#1)#2#{\[email protected]@i(#1){#2}}
\def\[email protected]@i(#1)#2#3{%
    \ifnum#1>0 \expandafter\[email protected]\else\expandafter\[email protected]\fi
    {\expandafter\[email protected]\expandafter{\expandafter\expandtimes\expandafter(\number%
      \numexpr#1-1)#2}}{#2}{#3}%
}
\long\def\[email protected]#1#2{#1}
\long\def\[email protected]#1#2{#2}
\long\def\[email protected]#1#2{#2{#1}}
\long\def\[email protected]#1#2{\expandafter\[email protected]\expandafter{#2}{#1}}
\makeatother
%

that, if added to the preamble, would allow the multi-expansion syntax to be condensed to

\expandtimes(3)\cmdwithexpandedargs{\expandafter\envnum\itemnum}

to achieve the same result.

Category: macros Time: 2016-07-28 Views: 0

Related post

  • Use the counter value as an argument 2014-09-09

    Do you know a simple way to use the current counter value to call the argument in a macro? In myPackage I have this Note: I have the for loop defined as % \forloop[step]{counter}{initial_value}{conditional}{code_block} The problem is in the following

  • Save counter value inside command? 2014-03-06

    I'm trying to automatically generate commands based on the value of a counter: \documentclass{article} \newcounter{count} \newcommand\generator[1]% {% \stepcounter{count}% \expandafter\def\csname#1\endcsname{\thecount}% } \generator{one} \generator{t

  • Save current value of counter in a command 2012-01-04

    I would like to define kind of an own index, that collects certain information and one counter value - in the following small example the \addstuff-command that may get the value of a counter as its first argument and a description as its second. The

  • Count number of paragraphs in command argument 2011-04-21

    Is there a way to count the number of paragraphs in a command argument? I would like to make a command doing something like this: \newcounter{\numberofparagraphs} \newcommand{\testcmd}[1]{ \setcounter{\numberofparagraphs}{ ???? } \ifthenelse{\equal{\

  • Passing current counter value to stack data structure 2014-09-29

    I'm trying to combine a stack data structure with a \newcounter{} definition so that I can automatically pair together a collection of points scattered throughout my text. Each point is either marked as an \openbracket or a \closebracket, and automat

  • when ssh is run with a server + command argument, what startup profile runs if any? 2015-04-07

    I know /etc/profile and ~/.profile are among the start-up scripts used when ssh is used to login to a server – i. e. using ssh with server argument. However, the scenario where I am just using ssh with a server+command argument does not seem to pick

  • Bash Command Arguments ${-} 2015-10-30

    I noticed this type of command argument in a bash script: node ${debug?--nocrankshaft --nolazy --nodead_code_elimination --debug-brk=15454} app.js I am wondering what ${....} means? How is it used? Also, is there a good source out there that discusse

  • How can I write section counter value : section title : page : file : line to the log whenever a macro is called? 2016-06-22

    I am working on context-based log entries. Each time a selected (patched) macro is called, it should provide its contents along with a meaningful log entry. "Meaningful" log entry defined: The following context is given per custom log entry: sec

  • Solaris kstat sdX disk nread counter value decreasing 2009-12-21

    I get strange disk io nread (bytes read) counter values (from kstat) on Solaris. Example of collected nread value for sd6 disk collected in 30s interval (command kstat -n sd6): 768579416 768579416 768579416 768579416 768579416 768579416 768579416 768

  • How to use a counter value in a hypertarget name? 2011-04-30

    I am trying to mark places in my document automatically (for later bookmarking). I defined a counter nops and my marking macro should be like this: \newcommand{\nop}{\addtocounter{nops}{1}\hypertarget{nop\value{nops}}{}}, but it seems that this is no

  • How to execute \LTXexample repeatitively with a looping macro such that its counter value gets rendered in both input/output parts? 2011-08-11

    The following code is the simplified version of my real scenario. It might look trivial. I forgot to say that the following code is the Minimal Not Working Example (MNWE): \documentclass{article} \usepackage{showexpl} \usepackage{graphicx} \begin{doc

  • How to make spaces as command argument delimiters? 2011-12-19

    How to make spaces as command argument delimiters? \documentclass{article} \newcommand\foo[2]{#1 and #2.} \begin{document} \foo hi you \end{document} The expected output is hi and you. Of course I don't want to enclose the arguments with curly braces

  • Using counter values as part of style name for plots 2012-04-05

    I figured I should be able to give a bunch of line styles and access them using counters. Sure enough, this works out fine; this document \documentclass{article} \usepackage{tikz,pgfplots} \tikzset{% Line 1/.style={color=blue,dashdotted}, Line 2/.sty

  • Using Perfmon on Windows Server 2008 R2 Which counter value should be used for physical memory usage? 2013-02-07

    I am attempting to use Performance Monitor to set up a relevant User Defined Dataset on a Windows 2008 R2 Server. This is to track the performance costs incurred by an application that my team is developing. One of the criteria I would like to track

  • How to store a counter value at some point in the document? 2013-11-14

    I'm beginning to learn LaTeX and I need some help with counters. My goal is to store the value a certain counter reaches at some point in the document and use it later. This is my attempt: \documentclass{article} \begin{document} \section{title} text

  • When writing an iptables save file by hand, how does one calculate the packet-counter and byte-counter values? 2014-02-11

    I am attempting to write a 22MB iptables rule-set file, the very kind that is generated with the iptables-save command, except endlessly long. Understandably, this takes a prohibitive amount of time. Take this miniature example. *filter :INPUT ACCEPT

  • `x2gostartagent` `command` argument specification 2014-03-03

    Regarding x2go, a remote X system, that I am using with an ubuntu client and a headless ubuntu server with the ubuntu-desktop is the following question. I gather that on the server I have to issue a command such as: x2gostartagent geometry link_quali

  • How to build a macro that return the absolute value of an argument of another macro? 2014-06-14

    How can I build a macro that return the absolute value of an argument of another macro? I would like that the \abs macro return the absolute value of the its argument. \RequirePackage{tikz} \newcommand{\abs}[1]{<implementation>} \newcommand{\command

  • Keeping rows ordered by counter value 2014-09-17

    I'm storing linked words in a Cassandra v2 cluster and I'm having a hard time doing efficient reads. I'm trying to fetch the top X rows with the highest counter value. Cassandra doesn't store the rows in any ordered way, so simply adding LIMIT X to t

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.823 (s). 13 q(s)