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}