Like other programming languages, the best way to to use a value multiple times in LaTeX is to define a variable for it. Later when you want to change the value, you only need to change it once. Here is how to do it.
Use \newcommand
#
We can use \newcommand
to define a new command for the text like this:
\newcommand{\toyCommand}{some text}
In the main, to insert the text, we can call this command:
We can use \toyCommand{} like this.
Or we can use \toyCommand\ like this.
Note that the double bracket {}
or escaped space \
after the custom command is necessary,
without which, the inserted text and text after it will stick together with no space.
If we just want to use \toyCommand
, we can use xspace package and add space when defining the command:
\usepackage{xspace}
\newcommand{\toyCommand}{some text\xspace}
Note that xspace
do have some drawbacks, see discussion here.
Use \def
#
We can also use \def
to define a variable and use it later1.
\def \myVar {some text}
We can use \myVar{} like this.
Note that {}
is also required to add a literal sapce after the inserted text.
References#
- Space after LaTeX commands: https://tex.stackexchange.com/q/31091/114857
- Is there any way I can define a variable in LaTeX: https://stackoverflow.com/q/1211888/6064933
It may override existing macro without warning, so use it with care. ↩︎