4

I want to store values, texts, commands... in variables to use them later. For example, I would like to have something like

\documentclass{article}
<preamble>
\begin{document}
\parta{A}
\partc{C}
\partb{B}
\partd{D}
\printit
\end{document}

where the commands \partx should be defined in the preamble and the arguments A,B,C,D are passed to variables that are used in the command \printit. More precisely, the \printit command could be to print the arguments A,B,C,D in a certain style and a certain order (not neccesarily the one given in the document). I am aware of \newcommand but I am not sure if this can be of help here?

1 Answer 1

6

this seems to be the same model as as \author{...} , \title{...} etc which save their arguments for use by \maketitle.

For that you just need

\newcommand\parta[1]{\newcommand\zzparta{#1}}
\newcommand\partb[1]{\newcommand\zzpartb{#1}}

\newcommand\printit{ blah \zzpartb\ and foobar \zzparta.}
8
  • 1
    Hi, I vote your answer +1. I vote always positive for my friends.
    – Sebastiano
    Oct 12, 2017 at 17:33
  • That's not a good answer. The command will evaluate when it is called, not when it is defined. So this is not a variable in a traditional language sense. If the command never gets called, the "body" of the command will never evaluate and if the command is called twice, the "body" will be evaluated twice.
    – A4L
    Oct 19, 2020 at 20:04
  • 2
    @A4L I disagree, but that's fine it is a big world. This is exactly the mechanism used in similar commands like \title and \author which store information in internal commands for use by \maketitle, which is basically the use case described in the question. Whether you call the internal tokens variables (or whether you call the top level commands commands) is not really relevant, TeX does not have variables or commands they are all macros really, so the terminology mapping helps if it helps but is never that exact.) Oct 19, 2020 at 20:37
  • @DavidCarlisle You're right. But wouldn't this create confusion if the end user were to execute an external script and wanted to store it's output in the variable? When \newcommand is used, the script would be called as many times as the command is called, but when (x|e)def is called, the script would only be called once and the output of it would be stored in the variable. Is that correct or am I wrong? Oh, and by "executing a script" I mean using \input and instead of writing a filename writing a pipe symbol and then the script to be executed. \input|./script.sh
    – A4L
    Oct 20, 2020 at 21:22
  • @A4L sorry I can't guess what you mean by the comment. The behaviour of a command defined by \newcommand and a command defined by \def is identical. and \edef can not be used with arbitrary latex text only in very specific low level code usage, (any accented character for example will fail in an edef) Oct 20, 2020 at 21:55

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.