2

I am using this code to align equation units of right the equation and to create nice equation legends.

Minimum Working Example:

\documentclass{article}
\usepackage{amsmath}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{siunitx}

%--------------------------------------------------------------------------------------------
% ---- The following part is to align the units of right the equations: ---------------------
%--------------------------------------------------------------------------------------------

\makeatletter%
\providecommand\add@text{}%
\newcommand\addunit[1]{%
    \gdef\add@text{[\si{#1}]\gdef\add@text{}}}% 
\renewcommand\tagform@[1]{%
    \maketag@@@{\llap{\add@text\qquad}(\ignorespaces#1\unskip\@@italiccorr)}%
}%
\makeatother%

%--------------------------------------------------------------------------------------------
% ---- The following part is for equation legends including symbol, description and unit ----
%--------------------------------------------------------------------------------------------

\newenvironment{legend*}{%
    \par\vspace{\abovedisplayskip}\noindent%
    \begingroup%
    \leftskip = 3em%                                                    Indent from left, alternative 4em
    \noindent\text{\hspace{0.2em} where}%
    \par\vspace{\abovedisplayskip}\noindent%
    \tabularx{\columnwidth}{>{\(}l<{\)} @{\hspace{2em}} l @{\hspace{2em}} >{\raggedright\arraybackslash}>{[} c<{]}}}%
{\endtabularx\par\vspace{2em}%
    \par%
    \endgroup%
}%

%--------------------------------------------------------------------------------------------
% ---- Document part ------------------------------------------------------------------------
%--------------------------------------------------------------------------------------------

\begin{document}

    \begin{equation}
        V = BC       \addunit{\square\meter}
    \end{equation}

    \begin{legend*}
        V & Filter volume & \si{\cubic\meter}\\
        B & Density of the filter & \si{\kilo\gram\per\cubic\meter}\\
        C & Filter mass & \si{\kilo\gram}\\
    \end{legend*}

\end{document}

Screenshot of the result:

Screenshot of the current state


Description of the issue:

As you can see, this is looking nice but not perfect. Therefore I want to align the units of the legend flushed right in one line with the unit of the equation.


Screenshot of the desired state:

Screenshot of the desired state

How can I modify this code?

3

Use an X column:

\documentclass{article}
\usepackage{amsmath}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{siunitx}

%--------------------------------------------------------------------------------------------
% ---- The following part is to align the units of right the equations: ---------------------
%--------------------------------------------------------------------------------------------

\makeatletter
\providecommand\add@text{}
\newcommand\addunit[1]{%
  \gdef\add@text{(\si{#1})\gdef\add@text{}}
}
\renewcommand\tagform@[1]{%
    \maketag@@@{\llap{\add@text\qquad}(\ignorespaces#1\unskip\@@italiccorr)}%
}
\makeatother

%--------------------------------------------------------------------------------------------
% ---- The following part is for equation legends including symbol, description and unit ----
%--------------------------------------------------------------------------------------------

\newenvironment{legend*}{%
    \par\vspace{\abovedisplayskip}\noindent
    \begingroup
    \tabularx{\columnwidth}{
      @{\hspace{3em}}
      >{\(}l<{\)}
      @{\hspace{2em}}
      l
      @{\hspace{2em}}
      >{\raggedright\arraybackslash}>{\hfill(} X<{)\qquad\hphantom{\theequation}}
      @{}
    }\multicolumn{3}{@{\hspace{3em}}l@{}}{where}\\[1ex]
    }%
    {\endtabularx\par\vspace{2em}%
    \par
    \endgroup
}%

%--------------------------------------------------------------------------------------------
% ---- Document part ------------------------------------------------------------------------
%--------------------------------------------------------------------------------------------

\begin{document}

    \begin{equation}
        V = BC       \addunit{\square\meter}
    \end{equation}

    \begin{legend*}
        V & Filter volume & \si{\cubic\meter}\\
        B & Density of the filter & \si{\kilo\gram\per\cubic\meter}\\
        C & Filter mass & \si{\kilo\gram}\\
    \end{legend*}

\end{document}

enter image description here

| improve this answer | |
  • 1
    @Dave Fixed. I would not use this alignment, though; in the equation the unit should be next to the formula, say two quads from it; in the legend, just after the description. – egreg Dec 7 '18 at 13:20
  • Thanks a lot for your help! You said "in the equation the unit should be next to the formula, [...]; in the legend, just after the description." Oh, is this a rule or just an opinion? :-) – Dave Dec 7 '18 at 13:25
  • @Dave Personal opinion. Do as it is common in your field. – egreg Dec 7 '18 at 13:37
  • Yes, when thinking about it - I think you are right! It is better to keep them close to each other. But something else makes me headache: Do you have an idea on how to auto-write the third table column always as \si{<column content>}? How can we add that to the modification? It would be nice, so in the text there is no need to write \si{<unit>} anymore, just <unit>... – Dave Dec 7 '18 at 13:58
  • @Dave There's the s column type – egreg Dec 7 '18 at 14:05

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.