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

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

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}
newcommandx{30}
begin{document}
x
end{document}

Output:

30

Use def command:

def variable {Something thats 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.

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

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

Leave a Reply

Your email address will not be published.