This might have been asked before, but I am not sure what to search for.

I am often in the situation that I want to print identical portions of text (or images, tables, etc) on a single A4 page in order to hand them out to a group of people. So for instance I might want to print 2x4 identical tables on a single page.

In principle I could type one portion and copy/paste the others in my editor, putting all of them into a table or using minipages etc.

However I'd rather have the text only once in my tex-file, so that I can later edit it easily. So I would somehow tell LaTeX that I want to save this portion of code and reuse it later with another command.

I guess input/include could work, but I'd rather have all of it in a single file.

How can I achieve this?

  • 2
    That is what \def is for...\def\codeA{...}. Then, whenever you type \codeA, the defined code is substituted in its place. The particulars could vary a little based on the context of your usage (which you did not provide). If you want to save the rendered version, rather than the raw code to reproduce it, the \savestack macro (stackengine package) could be of use. It renders the code in a box whose name you provide. – Steven B. Segletes Aug 27 '18 at 15:34

You can save either the code itself in a \newcommand (in the more dangerous TeX world, you can use \def or else \long\def if it includes paragraph breaks) or you can save the rendered object for later recall, here done with \savestack (which is a savebox with streamlined usage syntax).

Generally, as egreg notes, these things should be defined at the top level, so there is no need of extending outside the current group. However, if you need to save code from within a local group, you can follow the lead of \global\renewcommand equivalent of \global\def or else, in the TeX world, use \gdef or \global\def to have the memory of the definition extend outside the current group.

Note that with \savestack, local changes in font, fontsize, color, etc. do not carry through to the previously rendered box, unlike code saved in a \def.

\parskip 1em
\newcommand\codeA{\begin{tabular}{c}This \\is \\a\\ test\end{tabular}}
\savestack\codeB{\begin{tabular}{c}This \\is ALSO \\a\\ test\end{tabular}}

Here is code A: \codeA

and code B: \codeB

\leavevmode\Huge\color{red} Now go Huge red

Here is code A: \codeA

and code B: \codeB


enter image description here

  • No, please, don't advertise \def – egreg Aug 27 '18 at 20:14
  • @egreg For these sorts of things, do you still recommend \newcommand (and or \renewcommand)?? – Steven B. Segletes Aug 27 '18 at 23:09
  • 1
    Definitely; I don't think a global version is needed, because those macros should be defined at the top level. But there's tex.stackexchange.com/a/51750/4427 – egreg Aug 28 '18 at 8:39

Instead of define a table/tabular environments in a macro, another option is write the tabular or table floats in external files (e.g. mytableA.tex and mytableB.tex) and then include them in the main document with \input (e.g., \input{mytableA} and \input{mytableB}).

IMHO there are some advantages with this approach:

  1. Cleaner source code in main document. Specially useful when there are a lot of big tables.

  2. Easy editing. You cannot introduce mistakes outside the tables while editing otr viceversa. If some edition break something, you know where is the problem.

  3. Sometimes you might need the same table in different documents. In this way the updates need only one edition, without any copy-and-paste.



\begin{tabular}{c}This \\is \\a\\ test\end{tabular}


\begin{tabular}{c}This \\is ALSO \\a\\ test\end{tabular}


\parskip 1em
Here is the table A: \input{mytableA}\par
and table B: \input{mytableB}\par
Again the table A: \input{mytableA}\par
Again the table B: \input{mytableB}

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.