Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Latex displays the command values by default - I am finding it more difficult to display the command names along with the values.

I have a list of commands that I would like to loop over and print each one's name and value.

\documentclass[11pt,letterpaper]{article}
\usepackage{tikz}  % defines foreach

\begin{document}

\def\one{this is one}
\def\two{this is two}
\def\three{this is three}

\def\CMDs{\one,\two,\three}  % list of commands I would like to loop over

\foreach \cmd in \CMDs
{
    \string\cmd = \cmd \\ % should be name = value
}

\end{document}

But the \string is not resolving the \cmd, its just printing cmd.

Even tried \expandafter\string\csname\cmd\endcsname = \cmd as suggested here - no use, it is expanding and printing the value on both sides, not the name.

Please advice on how to achieve this.

In case one would like to know the background, this is for my CVMaker project - to keep track of internal variables and dump their values on demand, as debug-aid for the package users.

share|improve this question

2 Answers 2

up vote 19 down vote accepted

You are lucky that \cmd is a macro that contains the target macro, thus one \expandafter solves the problem, it expands \cmd and reveals the target macro to \string:

\documentclass[11pt,letterpaper]{article}
\usepackage[T1]{fontenc}% to get correct backslash
\usepackage{tikz}  % defines foreach

\begin{document}

\def\one{this is one}
\def\two{this is two}
\def\three{this is three}

\def\CMDs{\one,\two,\three}  % list of commands I would like to loop over

\foreach \cmd in \CMDs
{
    \expandafter\string\cmd = \cmd\par % should be name = value
}

\end{document}

Result

Further remarks:

  • You can inspect a macro definition by \show, e.g.: After \show\cmd TeX stops and shows the meaning on the console:

    > \cmd=macro:
    ->\one .
    
  • Another method is \meaning. It works similar to \string, but instead of the token, it converts the meaning of the token to a string, e.g.:

    \typeout{\string\cmd=\meaning\cmd}%
    

    prints to the console/.log file:

    \cmd=macro:->\one 
    \cmd=macro:->\two 
    \cmd=macro:->\three 
    
  • If \cmd would be have assigned via \let, e.g.:

    \let\cmd=\one
    

    then \cmd has the same \meaning as \one. And the name \one cannot be derived from \cmd anymore.

share|improve this answer
    
Working perfectly. Thanks for the detailed explanation and quick reply. –  Gopalakrishna Palem Jul 22 '13 at 14:51
    
And what if I want to print the value of \dimen0 when I make before: \settowidth{\dimen0}{ObJeCt}? –  Andrestand Feb 13 at 12:58
1  
@Andrestand: \the can be used to get the register values: \the\dimen0 . –  Heiko Oberdiek Feb 13 at 13:18

Here's a flexible implementation where you can choose the mode for showing the command's meaning.

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn

% The user level command just hands its arguments to an inner function
% #1 is optional, default value empty; #2 is the list of commands
\NewDocumentCommand{\showcommands}{O{}m}
 {
  \gopa_show_commands:nn { #1 } { #2 }
 }

\cs_new_protected:Npn \gopa_show_commands:nn #1 #2
 {
  % start a group so changes to the meaning of functions
  % deriving from the key setting will be undone at the end
  \group_begin:
  % evaluate the first argument, which should be `mode=<value>'
  \keys_set:nn { gopa/commands } { #1 }
  % for each item in the second argument (represented by ##1)
  % execute the code below
  \clist_map_inline:nn { #2 }
   {
    {\ttfamily \token_to_str:N ##1~=~\gopa_show_mode:N ##1 }
    \par
   }
  \group_end:
 }

% a variant for `mode=expand'; \use:n just uses its argument
% so \use:V will expand the control sequence that follows it
% and brace the expansion, so \use:n will remove the braces
\cs_generate_variant:Nn \use:n { V }

% Set up the key/value pairs; just one key that can receive
% the values "show", "expand" or "meaning"; default is "expand"
\keys_define:nn { gopa/commands }
 {
  mode .choice:,
  mode / show    .code:n = \cs_set_eq:NN \gopa_show_mode:N \cs_show:N,
  mode / expand  .code:n = \cs_set_eq:NN \gopa_show_mode:N \use:V,
  mode / meaning .code:n = \cs_set_eq:NN \gopa_show_mode:N \cs_meaning:N,
  mode .initial:n = expand,
 }

\ExplSyntaxOff

\begin{document}

\def\one{this is one}
\def\two{this is two}
\def\three{this is three}

\showcommands{\one,\two,\three}

\showcommands[mode=meaning]{\one,\mbox}

\end{document}

Another possible choice is mode=show that will use the terminal for showing the command's meaning. Use mode=meaning when one of the commands in the list is not a parameterless macro.

enter image description here

share|improve this answer
    
Thanks egreg. Its working fine and looking tidy and nice. I should start looking into the Latex 3 - seems it is powerful (and complex). –  Gopalakrishna Palem Jul 23 '13 at 4:01
    
@GopalakrishnaPalem: LaTeX3 is not that complex like it looks. I just started using it. egreg recommended to read expl3.pdf (type texdoc expl3 in your terminal). That document give a short overview about the new syntax and some more. All (?) new command are described in interface3.pdf (texdoc interface3). –  Tobi Jul 23 '13 at 10:14
    
@Tobi: Thanks Tobi - thats helpful information. Will certainly look into the texdoc expl3 –  Gopalakrishna Palem Jul 23 '13 at 16:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.