如何在甘特图中添加水平线?

如何在甘特图中添加水平线?

我正在尝试使用pgfganttOverleaf 中的包制作甘特图。我有一个问题尚未解决。我想在每个组下面添加水平线。为了更好地解释自己。我得到了这个: 我得到了什么

我想要这个: 我想要的是

这是我正在使用的甘特图规格的开头:我将其添加到带有的论文文档中\documentclass{book}。我已经包含了我认为与甘特图相关的软件包,以简化项目中包含的大量软件包。

\documentclass{book}
\usepackage{pgfgantt}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{import}

\ganttset{group/.append style={black}, milestone/.append style={red},
 progress label node anchor/.append style={text=red}}

\begin{document}

 \begin{ganttchart}[%Specs
 y unit title=0.5cm,
 y unit chart=0.7cm,
 hgrid={*1{black}, *7{dotted}, *1{black},  *6{dotted}},
 vgrid, 
 x unit=0.5cm,
 title height=1,
 title label font=\bfseries\footnotesize,
 bar/.style={fill=lightgray},
 bar height=0.4,
 group right shift=0,
 group top shift=0.7,
 group height=.3,
 group peaks width={0.2},
 inline
 bar label node/.append style={text width=15cm},
 group label node/.append style={text width=15cm},
 milestone label node/.append style={text width=15cm},
 bar label node/.append style={align=left, text width=width("longest text I have")},
 ]{1}{12}  

\ganttgroup[inline=false]{{Obj. 1: Some text in here}}{1}{3}\\
\ganttbar[inline=false]{Task 1.1: Some text in here}{1}{1}\\
\ganttbar[inline=false]{Task 1.2: Some text in here}{1}{2}\\
\ganttbar[inline=false]{Task 1.3: Some text in here}{2}{2}\\
\ganttbar[inline=false]{Task 1.4: Some text in here}{2}{3}\\
\ganttbar[inline=false]{Task 1.5: CSome text in here}{3}{4}\\
\ganttbar[inline=false]{\textit{Paper 1}}{4}{4}\\
\ganttnewline


 \end{ganttchart}
 \end{document}

\ganttnewline我想在空格后在文本中添加一条水平线。

答案1

欢迎使用 TeX.SE。可以使用 添加线条\ganttnewline,它接受可选的格式化键,例如线条粗细和颜色,如下所示: 。如果不为使用的命令\ganttnewline[very thick, blue]设置任何键,则不会绘制任何线条。请参阅手册的第 2.4 节。\draw\ganttnewlinepgfgantt

通过测量最长条形标签的长度来调整线的左起始位置,然后将其存储在\myleft像这样: \pgfmathsetmacro\myleft{width("Task 1.1: Some text in here")+10pt}。然后我们更新\ganttnewline命令以用作\myleft线的起点(MWE 中的注释指示此替换发生的位置)。

其他清理工作包括删除两个bar label node键中的一个,因为一个键有text width=15cm,另一个键正在测量宽度,如上所述。通常,您只需定义最长标签的宽度,无论是group labelbar label还是milestone label。这里,有最长的标签,因此和bar label的文本宽度设置是多余的,也可以删除。group label nodemilestone label node

在此处输入图片描述

\documentclass[margin=3pt]{standalone}
\usepackage{pgfgantt}
\usepackage{graphicx}
\usepackage{xcolor}

\ganttset{group/.append style={black},
          milestone/.append style={red},
          progress label node anchor/.append style={text=red}}

% Find the left edge of the bar label by measuring the width of the longest label, and add 10pt
\pgfmathsetmacro\myleft{width("Task 1.1: Some text in here")+10pt}
% Redefine \ganttnewline to draw a line from a position equal to the length of the longest label 
\makeatletter
\renewcommand\ganttnewline[1][]{%
\begingroup%
\def\local@drawarg{#1}%
\def\@tempa{grid}%
\ifx\local@drawarg\@empty\else%
\ifx\local@drawarg\@tempa%
\def\local@drawarg{/pgfgantt/hgrid style}% 
\fi%
\pgfmathsetmacro\y@upper{%
\gtt@lasttitleline * \ganttvalueof{y unit title}%
+ (\gtt@currentline - \gtt@lasttitleline - 1)%
* \ganttvalueof{y unit chart}%
}
\expandafter\draw\expandafter[\local@drawarg]
(-\myleft pt, \y@upper pt) --  %<-- -\myleft pt replaces 0pt, which starts the line from the left edge of the canvas.
(\gtt@chartwidth * \ganttvalueof{x unit}, \y@upper pt);%
\fi%
\global\advance\gtt@currentline by-1\relax%
\ifgtt@intitle\global\advance\gtt@lasttitleline by-1\relax\fi%
\global\gtt@lasttitleslot=0\relax%
\endgroup%
}
\makeatother

\begin{document}

 \begin{ganttchart}[%Specs
 y unit title=0.5cm,
 y unit chart=0.7cm,
 hgrid={*1{black}, *7{dotted}, *1{black},  *6{dotted}},
 vgrid,
 x unit=0.5cm,
 title height=1,
 title label font=\bfseries\footnotesize,
 bar/.style={fill=lightgray},
 bar height=0.4,
 inline=false,
 group label node/.append style={align=left,text width=width("Task 1.1: Some text in here ")},
 bar label node/.append style={align=left,text width=width("Task 1.1: Some text in here ")},
 ]{1}{12}

\ganttgroup{Literature review}{1}{3} \ganttnewline[very thick, blue]
\ganttbar{Obj. 1: Some text in here}{1}{1}\\
\ganttbar{Task 1.1: Some text in here}{1}{1}\\ 
\ganttbar{Task 1.2: Some text in here}{1}{2}\\
\ganttbar{Task 1.3: Some text in here}{2}{2}\\
\ganttbar{Task 1.4: Some text in here}{2}{3}\\
\ganttbar{Task 1.5: Some text in here}{3}{4}\\
\ganttbar{\textit{Paper 1}}{4}{4} \ganttnewline[very thick, red]
\ganttbar{Task 1.1: Some text in here}{1}{1}
\end{ganttchart}
\end{document}

相关内容