## Is there any way I can define a variable in LaTeX?

### Question

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.

2013/01/21
1
358
1/21/2013 5:08:56 PM

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

2020/01/13
379
1/13/2020 4:20:25 PM

Use \def command:

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



2012/03/15

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...).

2009/08/03


2013/11/06

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.

2017/12/25