解决方案 1

解决方案 1

我想写一些类似的东西: 在此处输入图片描述

到目前为止,我尝试使用 tikzcd,但我无法为箭头提供正确的位置和长度。

答案1

解决方案 1

我会做

\documentclass{article}
\usepackage{amsmath} % \xleftarrow, \xrightarrow
\usepackage{stmaryrd} % \mapsfrom
\begin{document}
\[
\begin{array}{ccccc}
    A & \xleftarrow{f} & B & \xrightarrow{g} & C\\
    f(x) & \mapsfrom & x & \mapsto & g(x)
\end{array}
\]
\end{document}

在此处输入图片描述

您可以使用 Segletes 的精彩答案来改进此解决方案。


解决方案 2(我认为更好)

然而,我们可以使用 Ti 得到另一个解决方案Z.优点:可以改变箭头的大小;缺点:代码较长。

\documentclass[tikz,margin=3mm]{standalone}
\usetikzlibrary{decorations.markings,positioning}
\begin{document}
\begin{tikzpicture}[decoration={
    markings,
    mark={at position 0 with {\arrow{|}}}}
]
\node (B) {$B$};
\node [left=1.5cm of B.west] (A) {$A$};
\node [right=1.5cm of B.east] (C) {$C$};
\node [below=0.5cm of B.south] (x) {$x$};
\node [left=1.5cm of x.west] (f) {$f(x)$};
\node [right=1.5cm of x.east] (g) {$g(x)$};
\draw[postaction={decorate},->] (x)--(f);
\draw[postaction={decorate},->] (x)--(g);
\draw[->] (B)--(A) node[midway,above] {$f$};
\draw[->] (B)--(C) node[midway,above] {$g$};
\end{tikzpicture}
\end{document}

在此处输入图片描述

针对解决方案 #2 编辑 1

你是指这样的吗?

\documentclass[tikz,margin=3mm]{standalone}
\usetikzlibrary{decorations.markings,positioning}
\begin{document}
\begin{tikzpicture}[
    decoration={
        markings,
        mark={at position 0 with {\arrow{|}}}},
    every node/.style={rectangle,minimum height=.75cm}
]
\node (B) {$d(M,N)=\sqrt{(x_M-x_N)^2+(y_M-y_N)^2}$};
\node [left=1.5cm of B.west] (A) {$a^2+b^2=c^2$};
\node [right=1.5cm of B.east] (C) {$E=mc^2$};
\node [below=0.5cm of B.south] (x) {$x$};
\node [below left=0.5cm and 0pt of A.south east] (f) {$f(x)$};
\node [below right=0.5cm and 0pt of C.south west] (g) {$g(x)$};
\draw[postaction={decorate},->] (x)--(f);
\draw[postaction={decorate},->] (x)--(g);
\draw[->] (B)--(A) node[midway,above=-1ex] {\scriptsize $f$};
\draw[->] (B)--(C) node[midway,above=-1ex] {\scriptsize $g$};
\end{tikzpicture}
\end{document}

在此处输入图片描述

针对解决方案 #2 编辑 2

在我看来这更好

\documentclass[tikz,margin=3mm]{standalone}
\usetikzlibrary{decorations.markings,positioning}
\begin{document}
\begin{tikzpicture}[
    decoration={
        markings,
        mark={at position 0 with {\arrow{|}}}
    },
    every node/.style={rectangle,minimum height=.75cm}
]
\node (B) {$d(M,N)=\sqrt{(x_M-x_N)^2+(y_M-y_N)^2}$};
\node [left=1.5cm of B.west] (A) {$a^2+b^2=c^2$};
\node [right=1.5cm of B.east] (C) {$E=mc^2$};
\node [below=0.5cm of B.south] (x) {$x$};
\node [below left=0.5cm and 0pt of A.south east] (f) {$f(x)$};
\node [below right=0.5cm and 0pt of C.south west] (g) {$g(x)$};
\node [below right=0.5cm and 0pt of B.south west] (xl) {};
\node [below left=0.5cm and 0pt of B.south east] (xr) {};
\draw[postaction={decorate},->] (xl)--(f);
\draw[postaction={decorate},->] (xr)--(g);
\draw[->] (B)--(A) node[midway,above=-1ex] {\scriptsize $f$};
\draw[->] (B)--(C) node[midway,above=-1ex] {\scriptsize $g$};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

您可以使用tabstackengine它来获取适当的数学间距并对齐运算符。

\documentclass{article}
\usepackage{amsmath} % \xleftarrow, \xrightarrow
\usepackage{stmaryrd} % \mapsfrom
\usepackage{tabstackengine}
\TABstackMath
\setstacktabulargap{0pt}
\TABbinary
\begin{document}
\[
\tabularCenterstack{rrcll}{
    A & \xleftarrow{f} & B & \xrightarrow{g} & C\\
    f(x) & \mapsfrom & x & \mapsto & g(x)
}
\]
\end{document}

在此处输入图片描述

如果关系运算符的垂直对齐不是必需的,则简单的堆栈就可以了:

\documentclass{article}
\usepackage{amsmath} % \xleftarrow, \xrightarrow
\usepackage{stmaryrd} % \mapsfrom
\usepackage{tabstackengine}
\stackMath
\begin{document}
\[
\Centerstack{
    A \xleftarrow{f} B \xrightarrow{g} C\\
    f(x) \mapsfrom x \mapsto g(x)
}
\]
\end{document}

在此处输入图片描述

答案3

你实际上并不需要 tikz-cd – 简单alignat就可以了:

\documentclass{article}

\usepackage{amsmath, stmaryrd}
\usepackage{makebox} 

\begin{document}

\begin{alignat*}{2}
A & \xleftarrow{\makebox[10pt]{$\scriptstyle f$}} B & & \xrightarrow{\makebox[10pt]{$\scriptstyle g$}} C \\
f(x) & \longmapsfrom \makebox*{$B$}{$x$} & & \longmapsto g(x)
\end{alignat*}

\end{document} 

在此处输入图片描述

答案4

相同marmot 的回答,但列对齐方式不同。

\documentclass{article}
\usepackage{tikz-cd}
\begin{document}
\[
 \begin{tikzcd}[row sep=.5ex,
 column sep=1.5em,
 /tikz/column 1/.append style={nodes={anchor=base east}},
 /tikz/column 3/.append style={nodes={anchor=base west}}] 
  A & \arrow[l,"f" swap] B \arrow[r,"g"] & C \\
  f(x) & \arrow[l,mapsto] x \arrow[r,mapsto] & g(x) \\
 \end{tikzcd}
\]
Or another alternative:
\[
 \begin{tikzcd}[row sep=.5ex,
 column sep=1.5em,
 /tikz/column 1/.append style={nodes={text width=width("$f(x)$"), text centered}},
 /tikz/column 3/.append style={nodes={text width=width("$g(x)$"), text centered}}] 
  A & \arrow[l,"f" swap] B \arrow[r,"g"] & C \\
  f(x) & \arrow[l,mapsto] x \arrow[r,mapsto] & g(x) \\
 \end{tikzcd}
\]
\end{document}

在此处输入图片描述

相关内容