我有這個代碼:
\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
托比昂已經解釋了這個問題並展示瞭如何解決它 - 所以給他打綠色勾。我想建議一種更易於閱讀和維護的方法:對環境使用巨集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
將接受任意數量的項目 - 當然最多為頁面寬度。輸出與上面相同:
當然,您可以像托比昂一樣使用鏈條做類似的事情。
答案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}