如何按层级结构排列条件?

如何按层级结构排列条件?

如果只有一层,我知道如何用对齐、案例或数组对齐条件左侧的条件。但是,我不知道如何在具有多个级别的条件层次结构中执行此操作。例如,以下 Latex 代码只会对齐右侧的所有条件:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{equation*}
\text{Start} 
\left\{\begin{aligned} \text{Do A,} \quad \quad\quad \quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\text{if a happens} \\
\text{otherwise} \left\{\begin{aligned} \text{Try B,}
    \quad \quad \quad \quad \quad \quad \quad\quad\quad \text{if b occurs}\\
\text{Try C} \left\{\begin{aligned} \text{Perform C1}  \quad \quad \text{if
      c increases}\\
\text{Perform C2, } \quad \text{if c decreases}
\end{aligned} \right.
\end{aligned} \right.
\end{aligned} \right.
\end{equation*}
\end{document}

那么问题是,如何将所有的“if”条件对齐?

答案1

\LSG您可以通过重置长度(当前设置为)来更改行间距1.4\baselineskip。其余的只是堆叠,并将括号缩放到堆叠高度。

\documentclass{article}
\usepackage[usestackEOL]{stackengine}[2013-10-15]
\usepackage{scalerel}
\newcommand\rbsa[2]{\raisebox{#1}{\scaleleftright[2ex]{\{}{#2}{.}}}
\def\stacktype{L}
\def\stackalignment{l}
\newlength{\LSG}
\setlength{\LSG}{1.4\baselineskip}
\setstackgap{L}{\LSG}
\begin{document}
\savestack{\cstack}{\rbsa{.5\LSG}{\stackunder{Perform C1}{Perform C2,}}}
\savestack{\bcstack}{\rbsa{\LSG}{\stackunder[1.5\LSG]{Try B,}{Try C\cstack}}}
\savestack{\abcstack}{\rbsa{1.5\LSG}{\stackunder[2\LSG]{Do A}{otherwise\bcstack}}}
Start\abcstack~~~~~~\Centerstack[l]
{if a happens\\if b occurs\\if c increases\\if c decreases}
\end{document}

在此处输入图片描述

要获取 OP 引用的替代格式:

\documentclass{article}
\usepackage[usestackEOL]{stackengine}[2013-10-15]
\usepackage{scalerel}
\newcommand\rbsa[2]{\raisebox{#1}{\scaleleftright[2ex]{\{}{#2}{.}}}
\def\stacktype{L}
\def\stackalignment{l}
\newlength{\LSG}
\setlength{\LSG}{1.4\baselineskip}
\setstackgap{L}{\LSG}
\begin{document}
\savestack{\cstack}{\rbsa{.5\LSG}{\stackunder{Perform C1}{Perform C2,}}}
\savestack{\bcstack}{\rbsa{0.5\LSG}{\stackunder[1.5\LSG]{Try C\cstack}{Try B,}}}
\savestack{\abcstack}{\rbsa{1.5\LSG}{\stackunder[2\LSG]{Do A}{otherwise\bcstack}}}
Start\abcstack~~~~~~\Centerstack[l]
{if a happens\\if c increases\\if c decreases\\if b occurs}
\end{document}

在此处输入图片描述

答案2

multirow以下是使用该包及其同名命令的解决方案\multirow

在此处输入图片描述

\documentclass{article}
\usepackage{amsmath,multirow}
\begin{document}
\noindent
Original code:
\begin{equation*}
\text{Start} 
\left\{\begin{aligned} \text{Do A,} \quad \quad\quad \quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\text{if a happens} \\
\text{otherwise} \left\{\begin{aligned} \text{Try B,}
    \quad \quad \quad \quad \quad \quad \quad\quad\quad \text{if b occurs}\\
\text{Try C} \left\{\begin{aligned} \text{Perform C1}  \quad \quad \text{if
      c increases}\\
\text{Perform C2, } \quad \text{if c decreases}
\end{aligned} \right.
\end{aligned} \right.
\end{aligned} \right.
\end{equation*}

\bigskip\noindent
New code:
\begin{equation*}
\text{Start}
\left\{ \begin{array}{ll}
   \text{Do A} & \text{if a happens}\\
   \multirow{3}{*}{$ % restart math mode
       \text{otherwise}  
       \left\{ \begin{array}{l}
         \text{Try B}\\
         \text{Try C} 
           \left\{ \begin{array}{l}
              \text{Perform C1}\\
              \text{Perform C2}
           \end{array} \right.
       \end{array} \right. %
   $} %  end of scope of multirow
   & \text{if b occurs   } \\
   & \text{if c increases} \\
   & \text{if c decreases} 
\end{array} \right.
\end{equation*}
\end{document}

备注:此解决方案在结构上(尤其是嵌套的使用array)与@Werner 对先前问题的回答,除了multirow这里使用该包来正确排版otherwise字符串(以及后续材料)


附录:原帖者询问如何交换 TryB/TryC 项的顺序。为此,交换三个结构中间两行的顺序,array并 (ii) 重新排列“如果 b 发生”/“如果 c 增加”/“如果 c 减少”字符串的顺序。

在此处输入图片描述

\documentclass{article}
\usepackage{amsmath,multirow}
\begin{document}
\begin{equation*}
\text{Start}
\left\{ \begin{array}{ll}
   \text{Do A} & \text{if a happens}\\
   \multirow{3}{*}{$ % restart math mode
       \text{otherwise}  
       \left\{ \begin{array}{l}
         \text{Try C} 
           \left\{ \begin{array}{l}
              \text{Perform C1}\\
              \text{Perform C2}
           \end{array} \right. \\
         \text{Try B}
       \end{array} \right. %
   $} %  end of scope of multirow
   & \text{if c increases} \\
   & \text{if c decreases} \\
   & \text{if b occurs} 
\end{array} \right.
\end{equation*}
\end{document}

答案3

我发现给定的结构有点难以理解:一些 if 条件与它们控制的操作相差甚远。此外,似乎在“尝试 C”之前应该有另一个“否则”。我会重新组织整个事情:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{equation*}
\text{Start}
\ \begin{cases} \text{if a happens}& \text{Do A,} \\
                \text{otherwise}&
    \begin{cases}\text{if b occurs}& \text{Try B,}\\
                 \text{otherwise}& \text{Try C}
    \ \begin{cases}\text{if c increases}& \text{Perform C1,}\\
                   \text{if c decreases}& \text{Perform C2.}
      \end{cases}
    \end{cases}
  \end{cases}
\end{equation*}
\end{document}

相关内容