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.
add the following to you preamble:
Then you can just use
\newCommandName{}
in the textFor more info on
\newcommand
, see e.g. wikibooksFor 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
...).Use
\def
command:Be aware that
\def
overrides preexisting macros without any warnings and therefore can cause various subtle errors. To overcome this either use namespaced variables likemy_var
or fall back to\newcommand
,\renewcommand
commands instead.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
.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