甘特图具有多个标题,但标题之间有条形图

甘特图具有多个标题,但标题之间有条形图

社区,

我想为我的博士项目制定一个工作计划。目标是将“第 1 年”作为标题,然后是一些任务,在第 1 年的最后一项任务下面应该直接写上“第 2 年”的标题,然后是任务等。

这是我目前所写的内容:

\documentclass[a4paper, 11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amssymb,amstext}
\usepackage[english, ngerman]{babel}
\usepackage[hmargin=2.5cm,vmargin=2.5cm]{geometry}
\usepackage[dvipsnames]{xcolor}
\setitemize{leftmargin=*}
\usepackage{pgfgantt}
\begin{document}
\begin{ganttchart}[
x unit=0.8cm,
y unit title=0.8cm,
y unit chart=0.6cm,
vgrid,
hgrid,
bar/.append style={fill=MidnightBlue},
milestone/.append style={fill=SkyBlue, rounded corners=3pt}]{1}{12}
\gantttitle{\textbf{YEAR 1}: Entry Phase}{12} \\
\gantttitlelist{1,...,12}{1} \\
\ganttbar{Literature: research and review}{1}{7} \\
\ganttbar{Writing of exposé}{4}{6} \\
\ganttmilestone{Public presentation}{7} \\
\ganttbar{Adaption of experiment 1}{11}{12} \\
\ganttbar{Conference participation $\&$ networking}{11}{12}\\

这是我遇到的问题:

\gantttitle{\textbf{YEAR 2}: Research Phase}{12} \\
\gantttitlelist{1,...,12}{1} \\
\ganttbar{Experiment 1: conduction}{1}{2} \\
\ganttbar{Planning of experiment 2}{3}{4} \\
\ganttbar{Organizing a workshop}{9}{11} \\
\ganttmilestone{Workshop}{11}
\end{ganttchart}

图表中的条形图在标题 2 之前开始,看起来非常奇怪。有解决方案吗?还是我必须制作单独的甘特图?

感谢您的帮助!

最好的,丽莎

答案1

标题的垂直位置部分取决于 TeX 计数器的值\gtt@currentline。如果在第二个标题之前将计数器加 1,然后在之后减 2,图表看起来会更好。您可以通过以下方式进行添加

\makeatletter
\advance\gtt@currentline by 1
\makeatother

\advance\gtt@currentline by -2减法也类似。参见\makeatletter 和 \makeatother 起什么作用?\makeatletter了解/的解释\makeatother

我对此不太了解pgfgantt,所以可能还有更好的方法来获取围绕描述的框架,但我在下面的代码中展示了一种方法,其中框架是手动绘制的。

整个图表实际上比文本块本身要宽一些,所以我还在标签中增加了换行符Conference participation \& networking。为了能够使用\\换行符,我添加了align=right样式bar label node

代码输出

\documentclass[a4paper, 11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amssymb} % amsmath loads amstext
\usepackage[english, ngerman]{babel}
\usepackage[hmargin=2.5cm,vmargin=2.5cm]{geometry}
\usepackage[dvipsnames]{xcolor}
%\setitemize{leftmargin=*}
\usepackage{pgfgantt}
\begin{document}
\begin{center}
\begin{tikzpicture} % added
\begin{ganttchart}[
  x unit=0.8cm,
  y unit title=0.8cm,
  y unit chart=0.6cm,
  vgrid,
  hgrid,
  bar/.append style={fill=MidnightBlue},
  milestone/.append style={fill=SkyBlue, rounded corners=3pt},
  bar label font=\footnotesize, % reduce font size
  milestone label font=\footnotesize\itshape, % reduce font size
  bar label node/.append style={align=right}, % allows for line breaks with \\ in the labels
  milestone label node/.append style={align=right}, % allows for line breaks with \\ in the labels
  canvas/.append style={name=canvas} % to be able to use the canvas node as reference for drawing
]{1}{12}
    \gantttitle{\textbf{YEAR 1}: Entry Phase}{12} \\
    \gantttitlelist{1,...,12}{1} \\
    \ganttbar{Literature: research and review}{1}{7} \\
    \ganttbar{Writing of exposé}{4}{6} \\
    \ganttmilestone{Public presentation}{7} \\
    \ganttbar{Adaption of experiment 1}{11}{12} \\
    \ganttbar{Conference participation\\\& networking}{11}{12}\\ % <- added a linebreak in the label text
    \makeatletter
    \advance\gtt@currentline by 1
    \makeatother
    \gantttitle{\textbf{YEAR 2}: Research Phase}{12} \\
    \gantttitlelist{1,...,12}{1} \\
    \makeatletter
    \advance\gtt@currentline by -2
    \makeatother
    \ganttbar{Experiment 1: conduction}{1}{2} \\
    \ganttbar{Planning of experiment 2}{3}{4} \\
    \ganttbar{Organizing a workshop}{9}{11} \\
    \ganttmilestone{Workshop}{11}
\end{ganttchart}

% draw frame around the labels on the left
\draw [/pgfgantt/canvas,fill=none]
 ([yshift=-0.5\pgflinewidth]canvas.north west) -- 
 (canvas.north west -| current bounding box.west) |- 
 ([yshift=0.5\pgflinewidth]canvas.south west);
\end{tikzpicture}
\end{center}

\end{document}

相关内容