Typesetting formula beside explanation of variables
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:
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. Thes 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...
I eliminate the multirow
and tabular
s altogether. I use \hfill
to achieve equal spacing, a TABstack for the explanation, and a \vcenter
ed \hbox
for the formula. Don't forget the \noindent
.
\documentclass{article}
\usepackage{siunitx}
\usepackage{amsmath}
\usepackage{lipsum}
\usepackage{tabstackengine}
\begin{document}
\lipsum[1]
\vspace{\baselineskip}%
\noindent\hfill%
$\vcenter{\hbox{$Re = \dfrac{v\cdot d}{\nu}$}}$%
\hfill%
\alignCenterstack{%
$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}]$
}%
\hfill%
\vspace{\baselineskip}
\lipsum[2]
\end{document}
Easy to place in a macro, here as \explainedFormula
:
\documentclass{article}
\usepackage{siunitx}
\usepackage{amsmath}
\usepackage{lipsum}
\usepackage{tabstackengine}
\newcommand\explainedFormula[2]{%
\vspace{\baselineskip}%
\noindent\hfill%
$\vcenter{\hbox{$#1$}}$\hfill%
\alignCenterstack{#2}\hfill%
\vspace{\baselineskip}
}
\begin{document}
\lipsum[1]
\explainedFormula{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}]$
}%
\lipsum[2]
\end{document}
Simple not very versatile implementation. The environment must contain the command \description
(which is not executed as a command).
\documentclass{article}
\usepackage{multirow}
\usepackage{siunitx}
\usepackage{amsmath}
\usepackage{lipsum}
\usepackage{environ,pbox}
\NewEnviron{MaxD}{%
\expandafter\parseMaxD\BODY\endparseMaxD%
}
\long\def\parseMaxD#1\description#2\endparseMaxD{
\vspace{\baselineskip}%
\hfill%
\pbox{0.45\linewidth}{$#1$}%
\hfill%
\begin{tabular}{@{}r@{\dots}l@{}}%
#2%
\end{tabular}%
\hfill%
\vspace{\baselineskip}}
\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}
\begin{MaxD}
Re = \dfrac{v\cdot d}{\nu}
\description
$Re$& Reynolds number $[-]$\\
$v$& velocity of the fluid $[\si{\milli\meter\per\second}]$\\
$d$& characteristic linear dimension $[\si{\milli\meter}]$\\
$\nu$& kinematic viscosity $[\si{\milli\meter\squared\per\second}]$
\end{MaxD}
\lipsum[2]
\end{document}
To make it at least a bit more versatile, you could use the tabularx
package and use the following as definition of \parseMaxD
instead:
\usepackage{tabularx}
\long\def\parseMaxD#1\description#2\endparseMaxD{
\vspace{\baselineskip}%
\hfill%
\pbox{0.45\linewidth}{$#1$}%
\hfill%
\begin{tabularx}{0.5\linewidth}{@{}r@{\dots}X@{}}%
#2%
\end{tabularx}%
\hfill%
\vspace{\baselineskip}}
You could as well always typeset the formula in \displaystyle
using \pbox{0.45\linewidth}{$\displaystyle #1$}
.