Math Mode – Typesetting Formula Beside Explanation of Variables

macrosmath-modespacing

I've been using the below snippet for quite a while now to typeset important formulas in my documents. Until now I always copy-pasted the snippet and manually tweaked it until the spacing looked satisfying, but thats not why we're using LaTeX, right?

I'd love my snippet to even out spacings automatically (speaking the three marked spaces in the screenshot below should be the same size). Also some kind of macro that gives it a nicer input format with less redundant copy-pasting would be very nice (e.g. not having to type \vspace{\baselineskip}, \begingroup etc. every time). How can I achieve that? (I guess there are a lot more possibilities to polish it up so of course I'm open to other suggestions as well.)

\documentclass{article}
\usepackage{multirow}
\usepackage{siunitx}
\usepackage{amsmath}
\usepackage{lipsum}

\begin{document}
\lipsum[1]

\vspace{\baselineskip}
\begingroup
\setlength{\tabcolsep}{0pt}
\begin{tabular}{crl}
\multirow{4}{.3\textwidth}{%
\centering
$Re = \dfrac{v\cdot d}{\nu}$
}
&$Re$&\dots Reynolds number $[-]$\\
&$v$&\dots velocity of the fluid $[\si{\milli\meter\per\second}]$\\
&$d$&\dots characteristic linear dimension $[\si{\milli\meter}]$\\
&$\nu$&\dots kinematic viscosity $[\si{\milli\meter\squared\per\second}]$
\end{tabular}
\endgroup
\vspace{\baselineskip}

\lipsum[2]
\end{document}

Example screenshot

Best Answer

Edit Adding equation numbers and looking after \si{...}

The code below defines a command \ExplainedFormula that when given

\ExplainedFormula{\Re = \dfrac{v\cdot d}{\nu}}
     {\Re: Reynolds number:,
        v: velocity of the fluid: \milli\meter\per\second,
        d: characteristic linear dimension: \milli\meter,
      \nu: kinematic viscosity: \milli\meter\squared\per\second
     }
Nulla malesuada porttitor diam. Donec felis erat, congue non,
volutpat at, tincidunt tristique, libero. Vivamus viverra fermentum
felis. Donec nonummy pellentesque ante.
\ExplainedFormula*{\Re = \dfrac{v\cdot d}{\nu}}
     {\Re: Reynolds number:,
        v: velocity of the fluid: \milli\meter\per\second,
        d: characteristic linear dimension: \milli\meter,
      \nu: kinematic viscosity: \milli\meter\squared\per\second
     }

will produce output like this:

enter image description here

By default, \ExplainedFormula will add an equation number and there is a starred version, \ExplainedFormula*, that omits the equation number. The equation number is on the left, because this is where I always put it:) To put it on the right move the line \IfBooleanF{#1}{\refstepcounter{equation}\rlap{(\theequation)}} so that it is after the last \hfil in \ExplainedFormula and change the \rlap to \llap...perhaps I should do this automatically... `

Here is the full code:

\documentclass{article}
\usepackage{multirow}
\usepackage{siunitx}
\usepackage{amsmath}
\usepackage{lipsum}
\usepackage{etoolbox}
\usepackage{xparse}

\renewcommand\Re{\mathop{\textrm{Re}}}
\def\Explain#1:#2:#3!!!{% \Explain variable : description : unit !!!
  $#1$ & #2 $[\if\relax\detokenize{#3}\relax-\else\si{#3}\fi]$
  \\
}% add a line of explanation
\NewDocumentCommand\ExplainedFormula{ s m m }{% 
% usage: \ExplainedFormula{formula}{csv explanation}
  \begin{center}
  \hbox to\textwidth{%
        \IfBooleanF{#1}{\refstepcounter{equation}\rlap{(\theequation)}}
        \hfil$\displaystyle#2$
        \hfil\renewcommand*\do[1]{\Explain##1!!!}%
          \begin{tabular}{r@{\dots}l}\docsvlist{#3}\end{tabular}
        \hfil%
  }%
  \end{center}
}

\begin{document}

\ExplainedFormula{\Re = \dfrac{v\cdot d}{\nu}}
     {\Re: Reynolds number:,
        v: velocity of the fluid: \milli\meter\per\second,
        d: characteristic linear dimension: \milli\meter,
      \nu: kinematic viscosity: \milli\meter\squared\per\second
     }
Nulla malesuada porttitor diam. Donec felis erat, congue non,
volutpat at, tincidunt tristique, libero. Vivamus viverra fermentum
felis. Donec nonummy pellentesque ante.
\ExplainedFormula*{\Re = \dfrac{v\cdot d}{\nu}}
     {\Re: Reynolds number:,
        v: velocity of the fluid: \milli\meter\per\second,
        d: characteristic linear dimension: \milli\meter,
      \nu: kinematic viscosity: \milli\meter\squared\per\second
     }

\end{document}

Some words of explanation:

  • The \ExplainedFormula macro is defined using \NewDocumentCommand from the xparse package. The s m m specification says that \ExplainedFormula has three arguments, an optional * and two mandatory arguments. The equation number is not printed if the * is present.
  • The mandatory arguments of \ExplainedFormula are for the formula and a comma separated list of "explanations". In turn, each "explanation" consists of a variable followed by : followed by the explanation followed by another : and then the \si unit, which should be left blank if there is none (such as for the Reynolds number in the example above. The explanations should not contain !!! as this is used by \Explain to separate the formula and description components. The parts of the formulas should not be surrounded by $...$ as these are added by \ExplainedFormula
  • The comma separated list of explanations is processed using \docsvlist from the etoolbox package together with some trickery (via \Explain), to separate the formula component and the description. What happens is that \do gets given each part of the comma separated descriptions, such as \Re: Reynolds number:, and it gives this to \Explain with !!! added on the end so that the \Explain macro knows where #3 ends.
  • Everything is thrown inside an \hbox of width \textwidth and the components of the box are separated by \hfil's, each of which will expand equally to fill up the available space. This should give the required equidistant spacing
  • I have replaced Re by \Re, which is given by \renewcommand\Re{\mathop{\textrm{Re}}}
  • I don't really like the \dots in the explanation. I would probably add space either side, using something like @{\,\dots\,} in the tabular definition, or instead use a colon or equals sign...
Related Question