459

In LaTeX, how can I define a string variable whose content is used instead of the variable in the compiled PDF?

Let's say I'm writing a tech doc on a software and I want to define the package name in the preamble or somewhere so that if its name changes, I don't have to replace it in a lot of places but only in one place.

Smi
  • 13,850
  • 9
  • 56
  • 64
Andrea Giovacchini
  • 5,027
  • 3
  • 21
  • 23

5 Answers5

505

add the following to you preamble:

\newcommand{\newCommandName}{text to insert}

Then you can just use \newCommandName{} in the text

For more info on \newcommand, see e.g. wikibooks

Example:

\documentclass{article}
\newcommand\x{30}
\begin{document}
\x
\end{document}

Output:

30
Werner
  • 14,324
  • 7
  • 55
  • 77
second
  • 28,029
  • 7
  • 75
  • 76
  • 51
    @DiAlex be careful when omitting the curly braces, as it will interfere with white space after the inserted text. See [this question](http://tex.stackexchange.com/q/31091). – jtpereyda Jul 20 '12 at 17:30
  • I can't get this to work properly with non-ascii characters. Any ideas? – lindhe Jan 15 '16 at 20:29
  • 12
    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. – jg6 Oct 19 '20 at 20:05
  • 3
    Very upvoted, and it certainly serves the OP's purpose. But it is not really a variable. – myradio Mar 15 '21 at 08:43
  • Works, but compiler prints an error message: "Undefined control sequence". Is there a way to fix that? – ions me Apr 13 '22 at 01:09
  • @jg6 technically you are correct, this is not a variable, but a macro. But it serves the purpose. I could see no performance considerations about that on the internet. This is the LaTeX way, it's better to stick to the common / recommended practice. Can't mention myradio in the same comment... – Yaroslav Nikitenko Jun 14 '23 at 16:01
198

Use \def command:

\def \variable {Something that's better to use as a variable}

Be aware that \def overrides preexisting macros without any warnings and therefore can cause various subtle errors. To overcome this either use namespaced variables like my_var or fall back to \newcommand, \renewcommand commands instead.

plaes
  • 31,788
  • 11
  • 91
  • 89
  • 19
    Using `\def` can be problematic as it does not check for preexisting macros. See the [second circle of LaTeX hell](http://dabacon.org/pontiff/?p=6101) – as such, it is preferable to use `\newcommand` – Ryan Atallah Mar 15 '12 at 01:57
  • 2
    Thanks @RyanAtallah added note about that. Though, doesn't the meaning of `variable` mean that it's liable to change? :) – plaes Mar 15 '12 at 07:06
  • 7
    @plaes You're right, but that still doesn't mean you want to use `\def`. Instead, I think it's best practice to first "initialize" all variables that you want to use with `\newcommand` (even if it's just with `\@empty`), and then create your command to modify the variable using `\renewcommand`. This will let you know if your variable already exits, because `\newcommand` will throw an error (or at least a warning). – Nick2253 Oct 29 '12 at 01:30
  • 5
    Note that `\def` works for things in math mode, while `\newCommandName` does not. (E.g., `\def\mathExpression{\pi^2 + \sin x}`) – Jollywatt Apr 30 '17 at 09:16
  • 1
    using underscore messes up the drawing I had to do `myVar` – alper Mar 04 '21 at 22:12
45

For variables describing distances, you would use \newlength (and manipulate the values with \setlength, \addlength, \settoheight, \settolength and \settodepth).

Similarly you have access to \newcounter for things like section and figure numbers which should increment throughout the document. I've used this one in the past to provide code samples that were numbered separatly of other figures...

Also of note is \makebox which allows you to store a bit of laid-out document for later re-use (and for use with \settolength...).

dmckee --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
  • 35
    Simple usage example like `\newlength{\hcolw}` and `\setlength{\hcolw}{0.47\textwidth}` would be useful. – trybik Dec 22 '11 at 11:11
  • I join the previous comment; it's really hard to use this answer without additional search. Also note that this command should go after \begin{document}. – Yaroslav Nikitenko Nov 29 '22 at 12:49
28

If you want to use \newcommand, you can also include \usepackage{xspace} and define command by \newcommand{\newCommandName}{text to insert\xspace}. This can allow you to just use \newCommandName rather than \newCommandName{}.

For more detail, http://www.math.tamu.edu/~harold.boas/courses/math696/why-macros.html

Fran
  • 207
  • 2
  • 13
user1125069
  • 389
  • 3
  • 2
1

I think you probably want to use a token list for this purpose: to set up the token list \newtoks\packagename to assign the name: \packagename={New Name for the package} to put the name into your output: \the\packagename.

Roald Nefs
  • 1,302
  • 10
  • 29
John Burt
  • 136
  • 1