Is it possible to make a command or variable to store text and then later input it in the document?


\text1{This is text from the text1 variable and it is added in here.}
\text2{This is some other text from text2 variable.}


should give the following output enter image description here

  • 1
    Command names cannot contain numbers. You need to name them as \textOne, \textTwo, etc.
    – Jinwen
    Commented May 27, 2022 at 8:29
  • 2
    Also, \text is an existed (and widely used) command for producing normal text in math mode, so it is not advisable to redefine it.
    – Jinwen
    Commented May 27, 2022 at 8:31
  • @Jinwen Nevertheless it is possible to use \expandafter\def\csname text1\endcsname{This is text from the text1 variable and it is added in here.} and in the document body \csname text1\endcsname, but probably not advisable here. See the good answer of Ulrich Diez, tex.stackexchange.com/a/645732.
    – Stephen
    Commented May 27, 2022 at 11:24
  • @Stephen We need to understand that it is impossible to explain all TeX concepts in depth in a comment or two. Jinwen knows about things like \csname..\endcsname. My answer is, among other things, an attempt to further elaborate on what Jinwen's comments already addressed even before I started writing. Commented May 27, 2022 at 11:36
  • Not really related, but see also macros - Defining commands/abbreviations that contain numbers - TeX - LaTeX Stack Exchange and the linked question from it
    – user202729
    Commented May 27, 2022 at 14:38

3 Answers 3


\text is not the best choice of name for a home-brewed macro. \text is already defined and widely used in LaTeX.

The question touches on several aspects:

  1. The sequence \text (or respectively the sequence \text1 or the sequence \text2) is intended to serve two purposes simultaneously: First, assigning a "value" to a "variable". Second, to "output" the "value" of a "variable". This reminds me of the concept of overloading. This is not possible in TeX. In TeX, a control sequence cannot have different current definitions at the same time. One could define the sequence \text in such a way that it behaves - e.g. depending on a "flag" - differently at different times/that it behaves differently in different situations whose occurrence needs to be indicated by setting the corresponding flags. Or redefine the control-sequence right before beginning the document environment. With LaTeX so-called hooks can be used for automatizing the delaying of actions like redefining by in the preamble having a macro append to a "hook" instructions for redefining which will pend until the directives for starting the document-environment are processed. "hook" indicates that you can hook into what happens at those moments in time when certain things are done, e.g. starting the document-environemt, e.g., ending the document-environment,...

  2. When reading and tokenizing .tex-input, TeX only takes characters of category code 11(letter) for components of the names of control word tokens. (Simplified speaking control sequence tokens come in two flavors: If the name of the control word-token consists of a single character whose category currently is not 11(letter), then it is a control symbol token. In all other cases, e.g., if the name consists of several characters, it is a control word token.) The purpose of category code 11 is to allow a character that may appear in the .tex-input file to be used as component of the name of a control word token/of a control sequence token whose name may be formed by more than one character. For example, the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 do not normally have category code 11(letter) but 12(other) and cannot normally be used as components of names of control word tokens. Assigning them the category code 11(letter) seems obvious. However, I don't think this is recommended, because it could have unexpected effects in several places when processing the .tex input.

  3. In your question you refer to the concept "variable". If you're being nitpicky, the concepts "variable" and "value" are concepts, e.g., of higher-level programming languages used for creating executable programs. In TeX to some degree you can apply the concept of "variable" and "value". But when programming in TeX you need to implement this concept whereby with TeX underlying concepts like "tokenization", "token", "assignment", "macro", "expansion" and "macro-argument" play a rôle.

Depending on what exactly you want to be able to do with the "values" of the "variables", I suggest to simply use e.g. \newcommand to define macros which don't process arguments but just deliver replacement-text, where then the control word token defined as macro represents the "variable" and where with expanding the macro the tokens forming the replacement text of the macro form the "value" of the "variable".

E.g., you could do:


\newcommand\textA{This is text from the textA variable and it is added in here.}
\newcommand\textB{This is text from the textB variable and it is added in here.}

\section{First section}
\section{Second section}

If it is important to be able to use characters in the name of the "variable" / in the name of the control sequence token which do not have the category code 11(letter), e.g. also digits, I can offer a macro \CsNameToCsToken which is to collect a sequence of character tokens from the token stream and to replace this sequence of character tokens with the corresponding control sequence token, under the hood using \csname..\endcsname for doing this.

%% End \romannumeral-driven expansion safely:
%% Obtain control sequence token from sequence of explicit character tokens 
%% denoting the name of control sequence token:
%% \CsNameToCsToken<stuff not in braces>{NameOfCs}
%% yields
%% <stuff not in braces>\NameOfCs
%% (<stuff not in braces> may be empty/nothing.)


\CsNameToCsToken\newcommand{text1}{This is text from the text1 variable and it is added in here.}
\CsNameToCsToken\newcommand{text2}{This is text from the text2 variable and it is added in here.}

\section{First Section}
\section{Second Section}

This approach does not redefine the already defined and widely used control-word-token \text.

  • Thank you this is it. "text1" and "text2" were just arbitrary it could just as well have been "nameA" , "nameB". Your solution still works like this. Commented May 27, 2022 at 18:26

I'd give the scontents package a try. Enclose the part that you want to be able to repeat inside a scontents environment, and then recall it with \getstored.

text that you want to repeat


a_name is a name of your choice.


As you state it, you might simply use macros. Unfortunately, their name cannot contain digits, by default:

\newcommand\textTwo{More blabla}

\textOne % will display Blabla
\textTwo % will display More blabla
As we already said: \textOne

If you want to use "variable names" including digits (or other symbols), you can do so with \csname ...\endcsname:

\defText#1#2{\expandafter\newcommand\csname text:#1\endcsname{#2}} 
\useText#1{\csname text:#1\endcsname}

\defText{1}{This is the first paragraph.}
\defText{2}{This is the second paragraph.}



As we already said: \useText{1}

(In the present case, since "1" and "2" are single digits, you can even omit the {} around them, i.e., use: \defText1{...} and \useText1.)

More interesting, if you want to be able to define the text later in the document (e.g. in the References section) but use it earlier (in the main text), then you need roughly what the \label command does (namely, write the information into the .aux file which is read at the beginning of the (subsequent) compilation), with customized information instead of the current section name or equation label etc. Consider this:

% Define the label #1 to be #2, so \ref{X} gives Y:

Anticipating the result, we will prove in this paper that: \ref{text1}

From what precedes, we conclude:
\LABEL{text1}{We are the best.}

  • 1
    Redefining \@currentlabel permanently affects the \label...\ref-mechanism, thus I suggest doing things within a group/local scope: \def\LABEL#1#2{{\protected@edef\@currentlabel{#2}\label{#1}}}. Regarding the proper invoking of \@bsphack/\@esphack this is still not perfect. ;-) Commented May 28, 2022 at 9:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .