![将方程式的单位和相应图例的单位对齐](https://linux22.com/image/387217/%E5%B0%86%E6%96%B9%E7%A8%8B%E5%BC%8F%E7%9A%84%E5%8D%95%E4%BD%8D%E5%92%8C%E7%9B%B8%E5%BA%94%E5%9B%BE%E4%BE%8B%E7%9A%84%E5%8D%95%E4%BD%8D%E5%AF%B9%E9%BD%90.png)
我正在使用此代码来对齐方程的方程单位并创建漂亮的方程图例。
最小工作示例:
\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}
结果截图:
问题说明:
如您所见,这看起来不错,但并不完美。因此,我想将图例的单位与公式的单位对齐,使其在同一行上。
所需状态的屏幕截图:
我该如何修改此代码?
答案1
使用X
列:
\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}