我有以下 tikz 图。右侧的节点标签都对齐得很好,但左侧的节点标签却没有对齐。我该如何将这些节点的标签左对齐,并使所有线条都很好地显示出来?
\documentclass{standalone}
\usepackage{tikz}
\tikzset{ myblock/.style={draw,text width=20pt,minimum height=40pt,align=center}}
\begin{document}
\begin{tikzpicture}
\node[myblock] (L) {$\Lambda$};
\node[left=50pt,yshift=10pt] (A1) {$\mathsf{A}$};
\node[left=50pt,yshift=-10pt] (A'1) {$\mathsf{A}'$};
\node[left=50pt,yshift=-30pt] (B1) {$\mathsf{B}$};
\node[left=50pt,yshift=-50pt] (B'1) {$\mathsf{B}'$};
\node[right=50pt,yshift=10pt] (A2) {$\mathsf{A}$};
\node[right=50pt,yshift=-10pt] (A'2) {$\mathsf{A}'$};
\node[right=50pt,yshift=-30pt] (B2) {$\mathsf{B}$};
\node[right=50pt,yshift=-50pt] (B'2) {$\mathsf{B}'$};
\draw (A1) to ([shift={(0pt,10pt)}]L.west);
\draw (B1) to[out=0, in=180] ([shift={(0pt,-10pt)}]L.west);
\draw ([shift={(0pt,10pt)}]L.east) to (A2);
\draw ([shift={(0pt,-10pt)}]L.east) to[out=0, in=180] (B2);
\draw (A'1) to[out=0, in=180] ([shift={(0pt,-30pt)}]L.west) to ([shift=
{(0pt,-30pt)}]L.east) to[out=0, in=180] (A'2);
\draw (B'1) to (B'2);
\end{tikzpicture}
\end{document}
答案1
为什么不简单地进行align=left
一些小的尺寸调整(text width=.9em, inner xsep=1pt
)?
\documentclass{standalone}
\usepackage{tikz}
\tikzset{
myblock/.style={
draw,
text width=20pt,
minimum height=40pt,
align=center
},
mylittle/.style={
text width=.9em,
align=left,
inner xsep=1pt
}
}
\begin{document}
\begin{tikzpicture}
\node[myblock] (L) {$\Lambda$};
\node[left=50pt,yshift=10pt,mylittle] (A1) {$\mathsf{A}$};
\node[left=50pt,yshift=-10pt,mylittle] (A'1) {$\mathsf{A}'$};
\node[left=50pt,yshift=-30pt,mylittle] (B1) {$\mathsf{B}$};
\node[left=50pt,yshift=-50pt,mylittle] (B'1) {$\mathsf{B}'$};
\node[right=50pt,yshift=10pt] (A2) {$\mathsf{A}$};
\node[right=50pt,yshift=-10pt] (A'2) {$\mathsf{A}'$};
\node[right=50pt,yshift=-30pt] (B2) {$\mathsf{B}$};
\node[right=50pt,yshift=-50pt] (B'2) {$\mathsf{B}'$};
\draw (A1) to ([shift={(0pt,10pt)}]L.west);
\draw (B1) to[out=0, in=180] ([shift={(0pt,-10pt)}]L.west);
\draw ([shift={(0pt,10pt)}]L.east) to (A2);
\draw ([shift={(0pt,-10pt)}]L.east) to[out=0, in=180] (B2);
\draw (A'1) to[out=0, in=180] ([shift={(0pt,-30pt)}]L.west) to ([shift=
{(0pt,-30pt)}]L.east) to[out=0, in=180] (A'2);
\draw (B'1) to (B'2);
\end{tikzpicture}
\end{document}
答案2
你可以伪造空间。
\documentclass{standalone}
\usepackage{tikz}
\tikzset{ myblock/.style={draw,text width=20pt,minimum height=40pt,align=center}}
\begin{document}
\begin{tikzpicture}
\node[myblock] (L) {$\Lambda$};
\node[left=50pt,yshift=10pt] (A1) {$\mathsf{A}\phantom{'}$};
\node[left=50pt,yshift=-10pt] (A'1) {$\mathsf{A}'$};
\node[left=50pt,yshift=-30pt] (B1) {$\mathsf{B}\phantom{'}$};
\node[left=50pt,yshift=-50pt] (B'1) {$\mathsf{B}'$};
\node[right=50pt,yshift=10pt] (A2) {$\mathsf{A}$};
\node[right=50pt,yshift=-10pt] (A'2) {$\mathsf{A}'$};
\node[right=50pt,yshift=-30pt] (B2) {$\mathsf{B}$};
\node[right=50pt,yshift=-50pt] (B'2) {$\mathsf{B}'$};
\draw (A1) to ([shift={(0pt,10pt)}]L.west);
\draw (B1) to[out=0, in=180] ([shift={(0pt,-10pt)}]L.west);
\draw ([shift={(0pt,10pt)}]L.east) to (A2);
\draw ([shift={(0pt,-10pt)}]L.east) to[out=0, in=180] (B2);
\draw (A'1) to[out=0, in=180] ([shift={(0pt,-30pt)}]L.west) to ([shift=
{(0pt,-30pt)}]L.east) to[out=0, in=180] (A'2);
\draw (B'1) to (B'2);
\end{tikzpicture}
\end{document}
答案3
TeXnician 的巧妙技巧的替代方法是使用表格或数组进行对齐。这篇文章的重点是 LaTeX 具有非常强大的对齐工具,有时使用这些工具并添加 tikZ 内容作为覆盖会更容易。
\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\newcommand{\tikznode}[2]{%
\tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {$#2$};%
}%from https://tex.stackexchange.com/questions/402462/tikz-equivalent-of-pstricks-commands-ncbar-and-rnode/402466#402466
\tikzset{ myblock/.style={draw,text width=20pt,minimum height=40pt,align=center}}
\begin{document}
\[
\begin{array}{lp{4cm}l}
\tikznode{A1}{\mathsf{A}} & & \tikznode{A2}{\mathsf{A}} \\[0.7cm]
\tikznode{A'1}{\mathsf{A}'} & & \tikznode{A'2}{\mathsf{A}'} \\[0.7cm]
\tikznode{B1}{\mathsf{B}} & & \tikznode{B2}{\mathsf{B}} \\[0.7cm]
\tikznode{B'1}{\mathsf{B}} & & \tikznode{B'2}{\mathsf{B}'} \\[0.7cm]
\end{array}
\]
\begin{tikzpicture}[remember picture,overlay]
\draw (A1) -- (A2);
\path (A1) -- (A'2) node[midway,myblock,fill=white] (L) {$\Lambda$};
\draw (B1) to[out=0, in=180] ([shift={(0pt,-10pt)}]L.west);
\draw ([shift={(0pt,-10pt)}]L.east) to[out=0, in=180] (B2);
\draw (A'1) to[out=0, in=180] ([shift={(0pt,-40pt)}]L.west) to ([shift=
{(0pt,-40pt)}]L.east) to[out=0, in=180] (A'2);
\draw (B'1) to (B'2);
\end{tikzpicture}