使用alignat 對齊兩列以上?

使用alignat 對齊兩列以上?

考慮這個 MWE,修改自調整案例環境中的條件

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
  f(x) = \left\{\begin{alignedat}{3}
    & mx^2 +nx +1, &\text{if } & x \le -1 \\
    & 2m e^{|x|-1} + \sin \pi x - 3n, \qquad  &\text{if } -1 < {}&x < 1 \\
    & 3x^2 - (m+n)x, &\text{otherwise} &
  \end{alignedat}\right.
\]
\end{document}

結果是:

測試.png

我想要 sifotherwise對齊;和xs 對齊。

可以用 來完成嗎{aligned}?如果是這樣,我哪裡出錯了?

答案1

您需要使用&&來使條件保持對齊。為了實現x對齊,在這種情況下我建議使用\hphantom

在此輸入影像描述

筆記:

  • 您需要使用{-1}第二種情況,以便將-視為一元運算子而不是二元運算子。

從評論中回答您的問題:

  • 每個都&提供一個r左/l右對齊點。也就是文字&對齊且文字&對齊l。因此第一個&表達式向右對齊f(x) = {,後續表達式向左對齊。然後,希望對齊後續文字(條件的開頭)l。這意味著我們需要 &&.第一個&將給出r右對齊,第二個&確保我們有l左對齊。
  • 使用\hphantom是獲得所需對齊的簡單方法。當然,可以使用 a 來完成,但是由於文字的原因&,這將需要使用某種類型的巨集。請注意,不等式表達式與文字重疊。\lapotherwiseotherwise

這是其他兩種方法的結果:

在此輸入影像描述

代碼:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\noindent
Recommended approach: use a \verb|\hphantom{}|: 
\[
      f(x) = \left\{\begin{alignedat}{3}
        & mx^2 +nx +1,                            &&\text{if } \hphantom{-1 <{}}   x \le -1 \\
        & 2m e^{|x|-1} + \sin \pi x - 3n, \qquad  &&\text{if } {-1} < x < 1 \\
        & 3x^2 - (m+n)x,                          &&\text{otherwise} 
      \end{alignedat}\right.
    \]
Use additional \verb|&| instead of \verb|\hphantom{}|:
\[
  f(x) = \left\{\begin{alignedat}{4}
    & mx^2 +nx +1,                            &&\text{if } &      &&  &x \le -1 \\
    & 2m e^{|x|-1} + \sin \pi x - 3n, \qquad  &&\text{if } & {-1} &&{}< {}&x < 1 \\
    & 3x^2 - (m+n)x,                          &&\text{otherwise} 
  \end{alignedat}\right.
\]
With \verb|mathllap|:
\[
  f(x) = \left\{\begin{alignedat}{4}
    & mx^2 +nx +1,                            &&\text{if } &      &&  &x \le -1 \\
    & 2m e^{|x|-1} + \sin \pi x - 3n, \qquad  &&\text{if } & {-1} &&{}< {}&x < 1 \\
    & 3x^2 - (m+n)x,                          &&\text{\rlap{otherwise}} 
  \end{alignedat}\right.
\]
\end{document}

答案2

另一個選擇是使用cases環境。這裡需要更少的&符號,以對齊為代價x,但我個人認為這樣更優雅。

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
  f(x) = \begin{cases}
    mx^2 +nx +1, &\text{if }  x \le -1 \\
    2m e^{|x|-1} + \sin \pi x - 3n,  &\text{if } -1 < x < 1 \\
    3x^2 - (m+n)x,  &\text{otherwise}
  \end{cases}
\]
\end{document}

在此輸入影像描述

答案3

你可以用這個

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
  f(x) = \left\{\begin{alignedat}{3}
    & mx^2 +nx +1,                             &&\text{if }         &    & x \le -1 \\
    & 2m e^{|x|-1} + \sin \pi x - 3n, \qquad   &&\text{if }         &-1< & x < 1 \\
    & 3x^2 - (m+n)x,                           &&\text{otherwise}   &&
  \end{alignedat}\right.
\]
\end{document}

編輯:圖像:

測試.png

編輯(bbeeton 提供):調整後的影像:

調整後的程式碼輸出

<這裡的差異在於第二行左側標誌周圍的間距。這可以透過以下兩種方式之一來完成:

  • ... &-1<{} & x < 1
  • 透過利用這一事實X兩行的寬度相同,並將它們輸入為

    & mx^2 +nx +1,                             &&\text{if }         &     x &\le -1 \\
    & 2m e^{|x|-1} + \sin \pi x - 3n, \qquad   &&\text{if }         & -1< x &< 1 \\
    

僅當對齊 &在符號之前時,才能確保操作和關係符號周圍的適當間距。

相關內容