8

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

4 Answers 4

8

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...
7
  • +1 for the consideration of equation numbers. Wouldn't \mathop{\textrm{Re}} result in incorrect spacing? I'd use just \textrm{Re} as the definition of \Re. Also I think in the first point of your explanations you used the wrong seperator (in your code you use : but in the explanation /).
    – Skillmon
    Oct 28, 2017 at 12:41
  • \Re is no operator. It has nothing to do with real and imaginary part but is a dimensionless value. And I just meant, that I didn't waste a single thought on the equation numbers.
    – Skillmon
    Oct 28, 2017 at 12:51
  • @Skillmon Certain "important" variables I like TeX to treat as operators in equations so that they are displayed more prominently. I never use the Reynold's number but if I did then I would probably define it this way.
    – user30471
    Oct 28, 2017 at 13:04
  • Wow, nice input format, love it! I tried to do a minor addition myself but found that I don't understand the code well enough to get it to work (especially I don't get the exact effect of the!!!). What I'd like to add is the automatic addition of $[\si{ before and }]$ after the unit part. I imagine this might be easy with the use of a second separator between explanation and unit like the already used :? Could you help me you with that? If you have a nice idea for including equation numbers, that would be really useful as well.
    – MaxD
    Oct 28, 2017 at 16:28
  • @MaxD I have added equations and taken care of the \si units. In addition, there are a few more words of explanation.
    – user30471
    Oct 28, 2017 at 19:29
5

I eliminate the multirow and tabulars altogether. I use \hfill to achieve equal spacing, a TABstack for the explanation, and a \vcentered \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}

enter image description here

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}
3

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$}.

3

It's quite simple; but note \Ren for the Reynolds number (using Re in math mode will add unwanted space between the letters).

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

\newcommand{\Ren}{\mathit{Re}}

\begin{document}

\lipsum*[1]
\begin{equation}
\Ren = \frac{vd}{\nu}
\qquad
\begin{aligned}
\Ren &\dots\text{Reynolds number (--)}\\
v    &\dots\text{velocity of the fluid (\si{\milli\meter\per\second})}\\
d    &\dots\text{characteristic linear dimension (\si{\milli\meter})}\\
\nu  &\dots\text{kinematic viscosity (\si{\milli\meter\squared\per\second})}
\end{aligned}
\end{equation}
\lipsum[2]

\end{document}

enter image description here

Alternatively, with reduced vertical spacing and perhaps easier input:

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

\newcommand{\Ren}{\mathit{Re}}

\begin{document}

\lipsum*[1]
\begin{equation}
\Ren = \frac{vd}{\nu}
\qquad
\begin{tabular}{@{}>{$}r<{$}@{$\dots$}l@{}}
\Ren & 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{tabular}
\end{equation}
\lipsum[2]

\end{document}

enter image description here

If you prefer a single environment:

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

\newcommand{\Ren}{\mathit{Re}}
\newenvironment{formulaexplained}
 {\qquad\begin{tabular}{@{}>{$}r<{$}@{$\dots$}l@{}}}
 {\end{tabular}}
\newenvironment{explainformula}[1]
 {\begin{equation}#1\begin{formulaexplained}}
 {\end{formulaexplained}\end{equation}}
\newenvironment{explainformula*}[1]
 {\begin{equation*}#1\begin{formulaexplained}}
 {\end{formulaexplained}\end{equation*}}

\begin{document}

\lipsum*[3]
\begin{explainformula}{
  \Ren = \frac{vd}{\nu}
}
\Ren & 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{explainformula}
\lipsum*[3]
\begin{explainformula*}{
  \Ren = \frac{vd}{\nu}
}
\Ren & 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{explainformula*}
\lipsum[3]

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.