需要使用 Tikz 的时间线帮助

需要使用 Tikz 的时间线帮助

我是 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 无法识别它。

另外,我希望能够为列表的不同条目使用不同的颜色,但我觉得我只能输入一种颜色。例如,我希望表达式“我想要一个列表”为蓝色,而“使用不同的颜色”为橙色。

可以这样做吗?

答案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)事情就会变得更容易
  • 仅锚定伪鳕鱼(A.南在B.北上方)
  • 将常规 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}

关于 Open Workbench 的印象:

超

相关内容