我是 LaTeX 和 TikZ 的新手。
我需要繪製一個時間線,其中用不同顏色編寫的文字代表我的專案的不同並行步驟。
這是我的程式碼:
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[%
every node/.style={
font=\scriptsize,
% Better alignment, see https://tex.stackexchange.com/questions/315075
text height=1ex,
text depth=.25ex,
},
]
% draw horizontal line
\draw[-] (0,0) -- (15,0);
% draw vertical lines
\foreach \x in {0,1.25,...,15}{
\draw (\x cm,3pt) -- (\x cm,0pt);
}
\foreach \x in {0,5,...,15}{
\draw (\x cm,3pt) -- (\x cm,8pt);
}
% place axis labels
\node[anchor=north] at (0,0) {Sep. 2024};
\node[anchor=north] at (2.5,0) {T3};
\node[anchor=north] at (5,0) {Sep. 2025};
\node[anchor=north] at (7.5,0) {T7};
\node[anchor=north] at (10,0) {Sep. 2026};
\node[anchor=north] at (12.5,0) {T11};
\node[anchor=north] at (15,0) {Sep. 2027};
% Text markers
\draw[color=black, thick] (0,0.4) -- (0,0.7) node[above, text=blue] {Step 1};
\draw[color=black, thick] (7.5,0.2) -- (7.5,0.5) node[above, text=orange] {Example};
\draw[color=black, thick] (12.5,0.2) -- (12.5,0.5) node[above, text=ForestGreen] {Enseignement};
\draw[color=black, thick] (1.25, -0.2) -- (1.25, -0.5) node[below, text=blue] {I would like a list here With different colors};
% Legend
\node[draw, circle, fill=blue, inner sep=1pt, label={right:Valorisation}] at (4,-2) {};
\node[draw, circle, fill=orange, inner sep=1pt, label={right:Recherche}] at (6.5,-2) {};
\node[draw, circle, fill=ForestGreen, inner sep=1pt, label={right:Enseignement}] at (9,-2) {};
\end{tikzpicture}
\end{document}
我有兩個問題。
首先,對於每個刻度,我可以編寫以列表形式呈現的多個條目,但我不知道如何換行。例如,在第二個刻度中我想要
"I would like a list here
With different colors"
我嘗試在大括號內使用“ \\
”,但 TeX 無法識別它。
另外,對於我的 lsit 的不同條目,我希望能夠使用不同的顏色,但我覺得我只能輸入一種顏色。例如,我希望表達“我想要一個列表”為藍色,而“具有不同顏色”為橙色。
可以這樣做嗎?
答案1
如果您使用,align=center
則可以\\
在節點文字中使用。
要區分顏色,請\textcolor{<color>}{<text>}
在節點內部使用而不是使用該text
選項。
\documentclass{standalone}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[%
every node/.style={
font=\scriptsize,
% Better alignment, see https://tex.stackexchange.com/questions/315075
text height=1ex,
text depth=.25ex,
},
]
% draw horizontal line
\draw[-] (0,0) -- (15,0);
% draw vertical lines
\foreach \x in {0,1.25,...,15}{
\draw (\x cm,3pt) -- (\x cm,0pt);
}
\foreach \x in {0,5,...,15}{
\draw (\x cm,3pt) -- (\x cm,8pt);
}
% place axis labels
\node[anchor=north] at (0,0) {Sep. 2024};
\node[anchor=north] at (2.5,0) {T3};
\node[anchor=north] at (5,0) {Sep. 2025};
\node[anchor=north] at (7.5,0) {T7};
\node[anchor=north] at (10,0) {Sep. 2026};
\node[anchor=north] at (12.5,0) {T11};
\node[anchor=north] at (15,0) {Sep. 2027};
% Text markers
\draw[color=black, thick] (0,0.4) -- (0,0.7) node[above, text=blue] {Step 1};
\draw[color=black, thick] (7.5,0.2) -- (7.5,0.5) node[above, text=orange] {Example};
\draw[color=black, thick] (12.5,0.2) -- (12.5,0.5) node[above, text=ForestGreen] {Enseignement};
\draw[color=black, thick] (1.25, -0.2) -- (1.25, -0.5) node[below=3.5mm, align=center] {\textcolor{blue}{I would like a list here}\\ \textcolor{orange}{With different colors}};
% Legend
\node[draw, circle, fill=blue, inner sep=1pt, label={right:Valorisation}] at (4,-2) {};
\node[draw, circle, fill=orange, inner sep=1pt, label={right:Recherche}] at (6.5,-2) {};
\node[draw, circle, fill=ForestGreen, inner sep=1pt, label={right:Enseignement}] at (9,-2) {};
\end{tikzpicture}
\end{document}
答案2
解決方案
您可以做以下幾件事,而且可能還有更多。幾點說明:
- 增加了一些缺失的部分
- 減少了我眼睛的代碼噪音;-)
- 由於某種原因該
every node
聲明引起了麻煩,所以我暫時將其刪除 - 有多種方法可以使節點文字中斷;限制
text width
是其中之一,請參閱節點文本上的 pgfmanual - 我將它作為一個單獨的樣式引入
runtxt
- 如果你在這裡命名你感興趣的節點,
(A)
事情就會變得更容易 - 只是錨定偽codish(A.south超過B.north)
- 將常規 LaTeX 文字放入其中,例如列表
- 將它們堆疊在一起,這裡使用預設值,不間斷且居中
做出你的選擇。然而,還有一些事情要做:
- 你的圖例的定位(你可以把它當作一個
\pic
?) - 您的
every node
風格至少需要經過審查才能使用
一般建議
作為一般性評論,我不會過度繪製這樣的圖表。你知道嗎甘特圖,建立在 Tikz 上?
如果您真的有很多事情需要處理,也許對於顯示部分,您可能想了解專案管理軟體。如果你在 MS-Office 世界中沒有它,這個開放工作台也許值得一瞧 (https://sourceforge.net/projects/openworkbench/)。 // 請參閱下面的螢幕截圖,以了解 Open Workbench 的圖片搜尋。
\documentclass[border=3mm]{standalone}
\usepackage[dvipsnames]{xcolor} % <<<
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[%
% every node/.style={
% font=\scriptsize,
% % Better alignment, see https://tex.stackexchange.com/questions/315075
% text height=1ex,
% text depth=.25ex,
% },
runtxt/.style={text width=25mm}, % <<<
]
% draw horizontal line
\draw[-] (0,0) -- (15,0);
% draw vertical lines
\foreach \x in {0,1.25,...,15}{
\draw (\x cm,3pt) -- (\x cm,0pt);
}
\foreach \x in {0,5,...,15}{
\draw (\x cm,3pt) -- (\x cm,8pt);
}
% place axis labels
\node[anchor=north] at (0,0) {Sep. 2024};
\node[anchor=north] at (2.5,0) {T3};
\node[anchor=north] at (5,0) {Sep. 2025};
\node[anchor=north] at (7.5,0) {T7};
\node[anchor=north] at (10,0) {Sep. 2026};
\node[anchor=north] at (12.5,0) {T11};
\node[anchor=north] at (15,0) {Sep. 2027};
% Text markers
\draw[color=black, thick]
(0,0.4) -- (0,0.7)
node[above, text=blue] {Step 1};
\draw[color=black, thick]
(7.5,0.2) -- (7.5,0.5)
node[above, text=orange] {Example};
\draw[color=black, thick]
(12.5,0.2) -- (12.5,0.5)
node[above, text=ForestGreen] {Enseignement};
\draw[color=black, thick,runtxt] % <<<
(1.25, -0.2) -- (1.25, -0.5)
node[below, text=blue] (A)
{I would like a
list here With different colors};
% ~~~ demonstrating some options ~~~~~~~
\node[anchor=north,runtxt,teal] (B) at (A.south){
\begin{itemize} % use regular LaTeX, BUT NO \tikz
\item if you like it
\item and you want it
\item put a list on it
\end{itemize}
};
\node[anchor=north,red] (C1) at (B.south) {
This could be the first row
};
\node[anchor=north,red!100!orange!50,fill=yellow!20] (C2) at (C1.south) {
of your list
};
% Legend
\node[draw, circle, fill=blue, inner sep=1pt, label={right:Valorisation}] at (4,-2) {};
\node[draw, circle, fill=orange, inner sep=1pt, label={right:Recherche}] at (6.5,-2) {};
\node[draw, circle, fill=ForestGreen, inner sep=1pt, label={right:Enseignement}] at (9,-2) {};
\end{tikzpicture}
\end{document}
對開放工作台的印象: