1

What do you recommend to pass a string as an argument to a \newcommand-defined command and have it used verbatim in the command's body?

Small pseudo-example:

\newcommand{\linedraw}[2]{%
\draw (#1)--(#2); 
% and further, more complicated operations involving (#1) and (#2)
}

and then it should be possible to do the following:

\begin{tikzpicture}   

   % here imagine some code which defines each of the six nodes 
   % (a0) , (a1) , (a2) , (b0) , (b1) , (b2)

   % Now the goal is to be able to write
   \foreach \loopvariable in {0,1,...,2}{
   \linedraw{a\loopvariable}{b\loopvariable};
    } 
   % and have tikz understand it. 
\end{tikzpicture}

One challenge appears to be to make it possible to pass a backslash as part of the argument. That is, an essential point appears to be to be able to pass the string "b\loopvariable" as an argument. This is likely to have occurred to many people.

  • 4
    It would be really nice to have a compilable document, not just fragments, but a\loopvariable etc. seems to work there – user31729 May 16 '17 at 18:30
  • 1
    This usually works fine, so as Christian says, please make a full minimal example. It has nothing to do with verbatimness of the argument. If however, you are doing this inside the axis env from pgfplots, then you'll get spanked, and have to do things a little differently. (often wrapping the entire thing in \pgfplotsextra{...} helps. – daleif May 16 '17 at 18:44
  • 2
    you emphasise string but it isn't clear what you mean by string in this context as tex does not have strings. a\loopvariable is two tokens a and \loopvariable by verbatim is normally meant the parsing done by \verb etc where a\loopvariable is 14 tokens including a backslash, but then that can only be printed, not executed – David Carlisle May 16 '17 at 18:45
  • Thanks. You are right, it works. The question was asked before I tried it. Passing a backslash as part of an argument appeared so dangerous that I did not even try. – Peter Heinig May 16 '17 at 18:49
  • Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. – samcarter_is_at_topanswers.xyz Jul 13 '17 at 19:17
3

Like the commenters pointed out, this works. Here is a small compilable example.

\documentclass{amsart}
\usepackage{tikz}

\begin{document}
\newcommand{\linedraw}[2]{%

\draw[->] (#1.north west)--(#2.north west);
\draw[->] (#1)--(#2);
}
\begin{tikzpicture}
 \foreach \loopvariable in {0,1,...,2}{
  \node (a\loopvariable) at (\loopvariable,0) [] {};
  \node (b\loopvariable) at (\loopvariable,1) [] {};
  \linedraw{a\loopvariable}{b\loopvariable};
  }
\end{tikzpicture}
\end{document}
| improve this answer | |
  • It appears not to compile on this site though. – Peter Heinig May 16 '17 at 19:03
  • 3
    TeX (etc) codes are not compiled here, regardless what you post. That's by design. – user31729 May 16 '17 at 20:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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