小页面中方程式和文本的垂直对齐

小页面中方程式和文本的垂直对齐

我正在尝试定义一个\formule带有两个参数的命令 ( ):第一个是数学方程式,第二个是要在其右侧显示的文本。困难在于第二个参数(文本)可以是一行也可以是多行。因此我尝试使用minipage

但是,我找不到修复垂直对齐的方法:等式总是向下移动。

我在看:我之所以把它包括进来,是因为它可能会(或不会?)影响少数特定情况下的结果。但转变仍然存在……我看了看那里但是tabularx,它会垂直对齐等式的第一行和文本的第一行。

以下是 MWE:

\documentclass{article}
\usepackage{mathtools,amssymb}
\usepackage{tabularx}
\mathtoolsset{showonlyrefs}
\newlength\tmplengthA
\newlength\tmplengthB
\newcommand{\formule}[2]{%
    \setlength{\tmplengthA}{.3\linewidth}
    \setlength{\tmplengthB}{.66\linewidth}
    \par\noindent\begin{minipage}{\tmplengthA}
    \setlength\abovedisplayskip{0pt}
    \setlength\belowdisplayskip{0pt}
    \setlength\abovedisplayshortskip{0pt}
    \setlength\belowdisplayshortskip{0pt}
    \begin{align}
        \vphantom{\ddag}#1
    \end{align}\null\end{minipage}\hfill\begin{minipage}{\tmplengthB}
        \noindent\vphantom{\ddag}#2
    \end{minipage}\par}

\newcommand{\formuleB}[2]{%
    \setlength{\tmplengthA}{.3\linewidth}
    \setlength{\tmplengthB}{.66\linewidth}
    \par\noindent\begin{tabularx}{\linewidth}{@{}>{$\displaystyle}p{\tmplengthA}<{$}@{\hfill}p{\tmplengthB}@{}}
        \setlength\abovedisplayskip{0pt}
        \setlength\belowdisplayskip{0pt}
        \setlength\abovedisplayshortskip{0pt}
        \setlength\belowdisplayshortskip{0pt}
        \vphantom{\ddag}#1 & \vphantom{\ddag}#2
    \end{tabularx}\par}

\begin{document}
    Let's try the \verb|\formule| command with two single-line arguments:
    \formule{n_1\sin i_1 = n_2\sin i_2}{some text about the refractive indexes $n_1$ and $n_2$.}
    And with \verb|\formuleB|, we get:
    \formuleB{n_1\sin i_1 = n_2\sin i_2}{some text about the refractive indexes $n_1$ and $n_2$.}
    The former is bad, and the second quite good. However they do not match horizontally, although I would have expected it...

    Let's try with a longer text:
    \formule{v=\frac{d}{\Delta\!t}}{This formula is so simple that it hardly deserves any explanation. The understanding is straightforward and the notation are \emph{trivial}. So I'm not gonna comment it!}
    Same player codes again! \textit{(Why is the previous line that close to this line?)}
    \formuleB{v=\frac{d}{\Delta\!t}}{Are you really sure that you want me to comment it? The versatility of a code depends on the date of creation, but reduces with the time you spend without using it. QED!}
    The horizontal and vertical spacing of the former looks better...\\
    Odd, isn't it?
\end{document}

产生

在此处输入图片描述

答案1

我不明白为什么你要定义两个不同的\formule命令。我估计,一个命令就够了:

\documentclass{article}
\usepackage{mathtools,amssymb}
\mathtoolsset{showonlyrefs}
\usepackage{array}
\newcommand{\formule}[2]{
    \par\medskip\noindent%
\begin{tabular*}{\linewidth}{@{}
    >{\centering$\displaystyle}p{\dimexpr0.34\linewidth-2\tabcolsep}<{$} % <---
                               m{\dimexpr0.66\linewidth-2\tabcolsep}
                            @{}}
   #1   &   #2  
  \end{tabular*}\par\medskip\noindent}

\usepackage{lipsum}
\begin{document}
\section{Test}
    Let's try the \verb|\formule| command with single-line arguments:
\formule{\sin i_1 = n_2\sin i_2}
        {some text about the refractive indexes $n_1$ and $n_2$.}
    and width formula with fraction and one line of text:
\formule{c=\frac{\lambda}{T}}
        {also this formula is verticaly aligned}
    Let's try the \verb|\formule| command with a longer text:
\formule{v=\frac{d}{\Delta t}}
        {This formula is so simple that it hardly deserves any explanation. The understanding is straightforward and the notation are \emph{trivial}. So I'm not gonna comment it!}
    One more test with multi line equation system:
\formule{\begin{aligned}
            a + b & = c     \\
        a^2 + b^2 & \ne c^2
          \end{aligned}} 
        {Some dummy equations}
    Is the alignment of the equations now the same in all examples?
\end{document}

它给:

在此处输入图片描述

编辑: 从定义中\formule删除了所有不必要的代码,并使用pm列类型改进了方程的垂直对齐。后者在array包中定义。添加了两个新示例以测试其使用。

如果您在第一列中填写的方程式与第二列中的文本相比(视觉上)不是垂直居中,则可以使用以下方式手动移动文本raisebox

\formule{< some equation >}{\raisebox{...}{\parbox{\linewidth}{ ... }}}

但以我的观点(和品味)来看,这不是必要的。

相关内容