将 3 个方程与案例和方程编号对齐

将 3 个方程与案例和方程编号对齐

在下面显示的等式中,我希望将“if”文本对齐 - 目前这不起作用,因为使用了三个独立的数组环境。谢谢! 三个方程及案例

以下是生成上述方程的代码:

\documentclass[11pt]{scrreprt}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\begin{document}
\begin{align}
\label{eq:RawOffsetCalculation}
y_\text{offset raw}
&
=\left\{
\begin{array}{rl}
0
&
\quad\text{if }n_\text{offset}=0 \\
\\
\dfrac{\sum\limits_i^{n_\text{offset}}y_\text{raw}}{n_\text{offset}}
&
\quad\text{if }n_\text{offset}>0
\end{array}
\right. \\
\label{eq:CorrectedOffsetCalculation}
y_\text{offset corr}
&
=\left\{
\begin{array}{rl}
0
&
\quad\text{if }n_\text{offset}=0 \\
\\
\dfrac{\sum\limits_i^{n_\text{offset}}y_\text{corr}}{n_\text{offset}}
&
\quad\text{if }n_\text{offset}>0
\end{array}
\right.
\\
\label{eq:CumulativeOffsetCalculation}
y_\text{offset cum}
&
=\left\{
\begin{array}{rl}
0
&
\quad\text{if }n_\text{offset}=0 \\
\\
\dfrac{\sum\limits_i^{n_\text{offset}}y_\text{corr cum}}{n_\text{offset}}
&
\quad\text{if }n_\text{offset}>0
\end{array}
\right.
\end{align}
\end{document}

答案1

使用p列说明符代替 r:

\begin{array}{>{\RaggedLeft$}p{6em}<{$}l}

它需要$ ..$定义,因为它的内容是文本模式:

\documentclass[11pt]{scrreprt}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{array,ragged2e}
\begin{document}

    \begin{align}\label{eq:RawOffsetCalculation}
    y_\text{offset raw} & =\left\{
        \begin{array}{>{\RaggedLeft$}p{6em}<{$}l}
            0 & \quad\text{if }n_\text{offset}=0 \\[\normalbaselineskip]
    \dfrac{\sum\limits_i^{n_\text{offset}}y_\text{raw}}{n_\text{offset}} & \quad\text{if }n_\text{offset}>0
    \end{array} \right. \\
    \label{eq:CorrectedOffsetCalculation}
    y_\text{offset corr}
    &
    =\left\{ \begin{array}{>{\RaggedLeft$}p{6em}<{$}l}
      0 & \quad\text{if }n_\text{offset}=0 \\[\normalbaselineskip]
    \dfrac{\sum\limits_i^{n_\text{offset}}y_\text{corr}}{n_\text{offset}} & \quad\text{if }n_\text{offset}>0
    \end{array} \right.
    \\
    \label{eq:CumulativeOffsetCalculation}
    y_\text{offset cum} & =\left\{
    \begin{array}{>{\RaggedLeft$}p{6em}<{$}l}
      0 & \quad\text{if }n_\text{offset}=0 \\[\normalbaselineskip]
    \dfrac{\sum\limits_i^{n_\text{offset}}y_\text{corr cum}}{n_\text{offset}} & \quad\text{if }n_\text{offset}>0
    \end{array} \right.
    \end{align}

\end{document}

在此处输入图片描述

答案2

您可以使用cases环境来处理此类方程。对于您的问题,您可以定义multicases环境并使用phantom命令来获得正确的对齐:

\documentclass[11pt]{scrreprt}
\usepackage[T1]{fontenc}
%\usepackage[latin1]{inputenc}
\usepackage{amsmath}

\makeatletter
 \newenvironment{multicases}[1]
   {\let\@ifnextchar\new@ifnextchar
    \left\lbrace\def\arraystretch{1.2}%
    \array{@{}l*{#1}{@{\quad}l}@{}}}
   {\endarray\right.\kern-\nulldelimiterspace}
\makeatother

 \begin{document}
 \begin{align}
 y_\text{offset raw}&=
 \begin{multicases}{1}
  0,& \text{if }n_\text{offset}=0 \\
  \dfrac{\sum\limits_i^{n_\text{offset}}y_\text{raw\phantom{ cum}}}{n_\text{offset}},&\text{if }n_\text{offset}>0
 \end{multicases}\label{eq1}\\
 y_\text{offset corr}&=
 \begin{multicases}{1}
 0,&\text{if }n_\text{offset}=0 \\
 \dfrac{\sum\limits_i^{n_\text{offset}}y_\text{corr\phantom{ cum}}}{n_\text{offset}},&\text{if }n_\text{offset}>0
 \end{multicases}\label{eq2}\\
 y_\text{offset cum}&=
 \begin{multicases}{1}
 0,&\text{if }n_\text{offset}=0 \\
 \dfrac{\sum\limits_i^{n_\text{offset}}y_\text{corr cum}}{n_\text{offset}},&\text{if }n_\text{offset}>0
 \end{multicases}\label{eq3}
 \end{align}
 \end{document}

在此处输入图片描述

PS:label每个等式后都有注释。

答案3

这是另一种保持基本align/array设置的解决方案。通过 (a) 测量最大分数项的宽度和 (b) 将 0 项放在所述宽度的框中,可以实现所需的对齐。

我还想建议您(i)在三个方程之间插入一些空格,(ii)明确指出被求和的项对某个指标变量的依赖性i。(在您发布的代码中,i出现在求和符号下方,但不出现在其他地方。)

在此处输入图片描述

\documentclass[11pt]{scrreprt}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath} % for "align" environment

% define the largest fractional term and save its width to "\mywid"
\newcommand\bigfrac{%
  \dfrac{\sum\limits_{i=1}^{n_{\text{offset}}} y_{\text{corr\,cum},i}}{n_{\text{offset}}}}
\newlength\mywid
\settowidth\mywid{$\bigfrac$}  % save width of "\bigfrac" to "\mywid"

\begin{document}
\setcounter{chapter}{3}
\setcounter{equation}{13}
\begin{align}
y_\text{offset\,raw} &=\left\{
\begin{array}{r@{\qquad}l}
   \makebox[\mywid][r]{$0$} %% place "0" in a box of width \mywid
   & \text{if $n_{\text{offset}}=0$} \\[1.5ex]
   \dfrac{\sum\limits_{i=1}^{n_{\text{offset}}}y_{\text{raw},i}}{n_{\text{offset}}}
   &\text{if $n_{\text{offset}}>0$}
\end{array}\right. \label{eq:RawOffsetCalculation}\\[3ex]
y_\text{offset\,corr} &=\left\{
\begin{array}{r@{\qquad}l}
   \makebox[\mywid][r]{$0$} %% place "0" in a box of width \mywid
   & \text{if $n_{\text{offset}}=0$} \\[1.5ex]
   \dfrac{\sum\limits_{i=1}^{n_{\text{offset}}}y_{\text{corr},i}}{n_{\text{offset}}}
   & \text{if $n_{\text{offset}}>0$}
\end{array}\right. \label{eq:CorrectedOffsetCalculation}\\[3ex]
y_\text{offset\,cum} &=\left\{
\begin{array}{r@{\qquad}l}
   0 %% no special treatment needed for this term
   & \text{if $n_{\text{offset}}=0$} \\[1.5ex]
   \bigfrac  % re-use the macro (no need to type it in again, right?)
   & \text{if $n_{\text{offset}}>0$}
\end{array}\right. \label{eq:CumulativeOffsetCalculation}
\end{align}
\end{document}

相关内容