tikzcd 和 aligned,如何?

tikzcd 和 aligned,如何?

tikzcd我在尝试使和环境共存时遇到了问题aligned
特别是,当aligned在 内嵌套 时tikzcd,对齐完全错误,并被随机推到单元格的右侧。

例如,在下面的输出中,第一部分由aligned内部的 a完成tikzcd,第二部分由aligned内部的 a完成align*

在此处输入图片描述

这是代码

\documentclass[a4paper,11pt,fleqn,oneside]{book}
\usepackage{amsmath, amsfonts}
\usepackage{amsthm, amssymb}

\usepackage{tikz}
\usetikzlibrary{matrix, arrows, cd, patterns}

\begin{document}
    \[
    \begin{tikzcd}[baseline=(current  bounding  box.center), cramped, row sep = 0ex,
    column sep = 1.5em,
        /tikz/column 1/.append style={anchor=base east},
        /tikz/column 2/.append style={anchor=base west}]
        A\rar &B\\
        x \rar[mapsto] & \left\{\begin{aligned}
            &f(x), && \text{ if } x\in X \text{ and some other condition},\\
            &g(x), && \text{ otherwise.}
            \end{aligned}\right.
    \end{tikzcd}
    \]
    \begin{align*}
    A &\longrightarrow B\\
    x & \longmapsto \left\{\begin{aligned}
            &f(x), && \text{ if } x\in X \text{ and some other condition},\\
            &g(x), && \text{ otherwise.}
            \end{aligned}\right.
    \end{align*}
\end{document}

aligned有人知道如何获得嵌套时的正确行为tikzcd吗?

答案1

tikzcd环境中,&与平常含义不同。用于ampersand replacement保留原始含义。

\documentclass[a4paper,11pt,fleqn,oneside]{book}
\usepackage{amsmath,amssymb,amsthm}

\usepackage{tikz-cd}

\begin{document}

\[
\begin{tikzcd}[
  baseline=(current  bounding  box.center),
  cramped,
  row sep = 0ex,
  column sep = 1.5em,
  /tikz/column 1/.append style={anchor=base east},
  /tikz/column 2/.append style={anchor=base west},
  ampersand replacement=\&,
]
A\arrow[r] \& B\\
x \arrow[r,mapsto] \&
  \left\{\begin{aligned}
  &f(x), && \text{ if } x\in X \text{ and some other condition},\\
  &g(x), && \text{ otherwise.}
  \end{aligned}\right.
\end{tikzcd}
\]
\begin{align*}
A &\longrightarrow B\\
x & \longmapsto
  \left\{\begin{aligned}
  &f(x), && \text{ if } x\in X \text{ and some other condition},\\
  &g(x), && \text{ otherwise.}
  \end{aligned}\right.
\end{align*}

\end{document}

在此处输入图片描述

\arrow与使用\rar类似的命令相比,我更喜欢更一致的风格。

不过,你也许想使用cases

\documentclass[a4paper,11pt,fleqn,oneside]{book}
\usepackage{amsmath,amssymb,amsthm}
\usepackage{tikz-cd}

\begin{document}

\[
\begin{tikzcd}[
  baseline=(current  bounding  box.center),
  cramped,
  row sep = 0ex,
  column sep = 1.5em,
  /tikz/column 1/.append style={anchor=base east},
  /tikz/column 2/.append style={anchor=base west},
  ampersand replacement=\&,
]
A\arrow[r] \& B\\
x \arrow[r,mapsto] \&
  \left\{\begin{cases}
  f(x), & \text{if $x\in X$ and some other condition},\\
  g(x), & \text{otherwise.}
  \end{cases}\right.
\end{tikzcd}
\]

\end{document}

在此处输入图片描述


替代方法:定义一个具有默认含义cdaligned的环境&。但是,所有“内部”环境都需要相同的方法。

\documentclass[a4paper,11pt,fleqn,oneside]{book}
\usepackage{amsmath,amssymb,amsthm}
\usepackage{tikz-cd}

\newenvironment{cdaligned}{\catcode`\&=4 \aligned}{\endaligned}

\begin{document}

\[
\begin{tikzcd}[
  baseline=(current  bounding  box.center),
  cramped,
  row sep = 0ex,
  column sep = 1.5em,
  /tikz/column 1/.append style={anchor=base east},
  /tikz/column 2/.append style={anchor=base west},
]
A\arrow[r] & B\\
x \arrow[r,mapsto] &
  \left\{\begin{cdaligned}
  &f(x), && \text{ if } x\in X \text{ and some other condition},\\
  &g(x), && \text{ otherwise.}
  \end{cdaligned}\right.
\end{tikzcd}
\]

\end{document}

相关内容