I want to write a document with equations from different books. The problem is sometimes different books use different letter for the same physical dimension.
I would like to create something like a parameter for each physical dimension I need, in order to be able to change the letter referred to the physical dimension by changing it only once.
This is what I thought would worked:
\section{Test paragraph}
\begin{equation}\label{distance}
x
\end{equation}
\begin{equation}
\ref{distance} = speed * time
\end{equation}
I want the equation to be x = speed * time
if what's in the line below \label{distance}
is "x".
When the letter "x" is changed in the line under \label{distance}
to something different such as "y" I want the equation to become y = speed*time
.
\label{}
is only used for cross referencing, not for a text substitution.
LaTeX provides tools for creating so-called macros. These are just extra commands made by users. For instance \newcommand{\macro}[num]{<substitution>}
is one trivial way to create a macro but also limited. A more sophisticated as well as a recommended approach is \NewDocumentCommand{\macro}{<definitions>}{<subsitution>}
(see documentation). You will also come across \def\macro{<subsitution>}
. Whilte, \def
has its usage in low level LaTeX, it is most of times not recommended way to create macros and very prone to errors, e.g. one pitfall is that it never reports if an existing macro is overwritten.
In your case, add \newcommand\distance{x}
in the preamble and then every \distance
in text will be substituted by its definition.
Here's a simple example:
\documentclass{article}
\newcommand\distance{\ensuremath s}
\newcommand\speed{\ensuremath v}
\newcommand\ttime{\ensuremath t}
\begin{document}
\section{Test paragraph}
In the equation \ref{distance}:
\begin{itemize}
\item \distance{} denotes a distance
\item \speed{} denotes speed and
\item \ttime{} denotes time.
\end{itemize}
\begin{equation}\label{distance}
\distance = \speed \times \ttime
\end{equation}
\end{document}
There are a few things I should mention. First, note I use \ensuremath
. LaTeX has two modes: text and math. In both modes, formatting is different including fonts. \ensuremath
keep math formatting and allows for math expressions even if a macro is used outside the math environment.
You also note I use \distance{}
instead of \distance
. In text (paragraphs, lists etc.), LaTeX consumes all spaces followed by a macro without arguments. As such, \distance X
would be rendered as xX
, while appending brackets {}
prevents from that.