我有这个代码:
\documentclass{tufte-latex}
\usepackage{tikz}
\begin{document}
\section{Some variables to play with}
\subsection{Time}
\begin{tikzpicture}[place/.style={circle,draw=gray,fill=gray,very thick}]
\sffamily
% put points on graph
\node (n1) at (1,0) [place,label=below:{Alternate past}] {};
\node (n2) at (3,0) [place,label=below:{Present}] {};
\node (n3) at (5,0) [place,label=below:{Near future}] {};
\node (n4) at (7,0) [place,label=below:{Far future}] {};
% \foreach \x [evaluate={\y=int(\x+1);}] in {1,...,9}
\draw[gray,very thick] (1,0) -- (7,0);
\end{tikzpicture}
\subsection{Hard vs. soft SF}
\begin{tikzpicture}[place/.style={circle,draw=gray,fill=gray,very thick}]
\sffamily
% put points on graph
\node (n1) at (1,0) [place,label=below:{2001}] {};
\node (n2) at (3,0) [place,label=below:{Alien}] {};
\node (n3) at (5,0) [place,label=below:{Blade Runner}] {};
\node (n4) at (7,0) [place,label=below:{WOTW}] {};
% \foreach \x [evaluate={\y=int(\x+1);}] in {1,...,9}
\draw[gray,very thick] (1,0) -- (7,0);
\end{tikzpicture}
\subsection{Optimism vs. pessimism}
\begin{tikzpicture}[place/.style={circle,draw=gray,fill=gray,very thick}]
\sffamily
% put points on graph
\node (n1) at (1,0) [place,label=below:{Star Trek}] {};
\node (n2) at (3,0) [place,label=below:{Neutron Star}] {};
\node (n3) at (5,0) [place,label=below:{WOTW}] {};
\node (n4) at (7,0) [place,label=below:{New Rose Hotel}] {};
% \foreach \x [evaluate={\y=int(\x+1);}] in {1,...,9}
\draw[gray,very thick] (1,0) -- (7,0);
\end{tikzpicture}
\end{document}
我想对齐三个连续体,使节点彼此对齐。最好的方法是什么?
答案1
原因当然是最左边的标签宽度不同。因此,一个解决方法是将text width
所有标签的设置为相同,例如
every label/.style={text width=3cm,align=center}
可能3cm
需要进行调整,具体取决于标签本身。
附录:严格来说,你只需要对最左边的节点执行此操作,正如 AboAmmar 指出的那样他的回答。
因此,您只需修改代码即可,例如对于第一个图表,
label={[text width=3cm,align=center]below:Alternate past}
或者,对于我下面发布的代码,text width=3cm,align=center
从样式中删除widelabels
,然后使用\node [place,"Alternate past" {widelabels,text width=3cm,align=center}] (n1) {};
只是为了好玩,这里建议使用chains
和quotes
库来绘制此类事物的替代方法。使用chains
您可以自动定位和连接节点,并且quotes
库提供了添加标签的替代方法。
\documentclass{tufte-handout}
\usepackage{tikz}
\tikzset{
place/.style={circle,draw=gray,fill=gray,very thick,on chain,join},
widelabels/.style={below,text width=3cm,align=center,font=\sffamily},
mychain/.style={start chain,node distance=2cm,every join/.style={gray, very thick}}
}
\usetikzlibrary{quotes,chains}
\begin{document}
\section{Some variables to play with}
\subsection{Time}
\begin{tikzpicture}[mychain]
% put points on graph
\node [place,"Alternate past" widelabels] (n1) {};
\node [place,"Present" widelabels] (n2) {};
\node [place,"Near future" widelabels](n3) {};
\node [place,"Far future" widelabels](n4) {};
\end{tikzpicture}
\subsection{Hard vs. soft SF}
\begin{tikzpicture}[mychain]
% put points on graph
\node [place,"2001" widelabels] (n1) {};
\node [place,"Alien" widelabels] (n2) {};
\node [place,"Blade runner" widelabels](n3) {};
\node [place,"WOTW" widelabels](n4) {};
\end{tikzpicture}
\subsection{Optimism vs. pessimism}
\begin{tikzpicture}[mychain]
% put points on graph
\node [place,"Star Trek" widelabels] (n1) {};
\node [place,"Neutron Star" widelabels] (n2) {};
\node [place,"WOTW" widelabels](n3) {};
\node [place,"New Rose Hotel" widelabels](n4) {};
\end{tikzpicture}
\end{document}
答案2
Torbjørn 已经解释了这个问题并展示了如何修复它 - 所以给他一个绿色勾号。我想建议一种更易于阅读和维护的方法:对环境使用宏tikzpicture
。
这是您的 MWE 的更紧凑版本:
\documentclass{tufte-book}
\usepackage{tikz}
%usage: \linegraph{item1,item2, ...}
\newcommand\linegraph[1]{%
\begin{tikzpicture}[place/.style={circle,draw=gray,fill=gray,very thick, radius=1mm},
every label/.style={text width=3cm,align=center}
]
\foreach \word [count=\x, remember=\x as \lastx] in {#1} {
\node (n\x) at (2*\x-1,0) [place,label=below:{\word}] {};
}
\draw[gray,very thick] (1,0) -- (2*\lastx-1,0);
\end{tikzpicture}
}
\begin{document}
\section{Some variables to play with}
\subsection{Time}
\linegraph{Alternate past, Present, Near future, Far future}
\subsection{Hard vs. soft SF}
\linegraph{2001, Alien, Blade runner, WOTW}
\subsection{Optimism vs. pessimism}
\linegraph{Star Trek, Neutron Star, WOTW, New Rose Hotel}
\end{document}
我已经将节点标签n1
、n2
、 .. 等放入\linegraph
,但按照目前写法,它们应该被删除,因为它们未被使用。此外,\linegraph
将接受任意数量的项目 - 当然最多为页面宽度。输出与上面相同:
当然,你可以像 Torbjørn 所做的那样,使用链条做类似的事情。
答案3
另一个简单的解决方法是只对齐最左边的三个标签,其他所有内容都将保持不变。例如,使用此命令\newcommand{\ALIGN}[1]{\makebox[6em]{#1}}
对齐这些节点。
\documentclass{article}
\usepackage{tikz}
\begin{document}
\section{Some variables to play with}
\subsection{Time}
\newcommand{\ALIGN}[1]{\makebox[6em]{#1}}
\begin{tikzpicture}[place/.style={circle,draw=gray,fill=gray,very thick}]
\sffamily
% put points on graph
\node (n1) at (1,0) [place,label=below:\ALIGN{Alternate past}] {};
\node (n2) at (3,0) [place,label=below:{Present}] {};
\node (n3) at (5,0) [place,label=below:{Near future}] {};
\node (n4) at (7,0) [place,label=below:{Far future}] {};
\draw[gray,very thick] (1,0) -- (7,0);
\end{tikzpicture}
\subsection{Hard vs. soft SF}
\begin{tikzpicture}[place/.style={circle,draw=gray,fill=gray,very thick}]
\sffamily
% put points on graph
\node (n1) at (1,0) [place,label=below:\ALIGN{2001}] {};
\node (n2) at (3,0) [place,label=below:{Alien}] {};
\node (n3) at (5,0) [place,label=below:{Blade Runner}] {};
\node (n4) at (7,0) [place,label=below:{WOTW}] {};
\draw[gray,very thick] (1,0) -- (7,0);
\end{tikzpicture}
\subsection{Optimism vs. pessimism}
\begin{tikzpicture}[place/.style={circle,draw=gray,fill=gray,very thick}]
\sffamily
% put points on graph
\node (n1) at (1,0) [place,label=below:\ALIGN{Star Trek}] {};
\node (n2) at (3,0) [place,label=below:{Neutron Star}] {};
\node (n3) at (5,0) [place,label=below:{WOTW}] {};
\node (n4) at (7,0) [place,label=below:{New Rose Hotel}] {};
\draw[gray,very thick] (1,0) -- (7,0);
\end{tikzpicture}
\end{document}