# How to reuse code

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?

• 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. Aug 27, 2018 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.

\documentclass{article}
\usepackage{stackengine,xcolor}
\parskip 1em
\begin{document}
\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

\end{document}


• No, please, don't advertise \def Aug 27, 2018 at 20:14
• 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 Aug 28, 2018 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.

MWEs:

mytableA.tex:

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


mytableB.tex:

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


main.tex:

\documentclass{article}
\parskip 1em
\begin{document}
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}
\end{document}


A variation of the answer given by @Steven B. Segletes, using the package scontents.

\documentclass{article}
\usepackage{xcolor}
\usepackage[store-cmd=copy]{scontents}
\begin{document}
\Scontents{\begin{tabular}{c}This \\is \\a\\ test\end{tabular}}
\Scontents{\begin{tabular}{c}This \\is ALSO \\a\\ test\end{tabular}}

Here is code A: \getstored[1]{copy}

and code B: \getstored[2]{copy}

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

Here is code A: \getstored[1]{copy}

and code B: \getstored[2]{copy}

\end{document}