如何左对齐、编号甘特图中的项目

如何左对齐、编号甘特图中的项目

我想在 latex 中创建甘特图,并将甘特图中的项目左对齐,并使用编号和子编号。但是,我以前从未遇到过这样的事情。如果有人能在这方面提供帮助,那将非常有帮助。

答案1

有点猜测,也没有子编号(不管你到底是什么意思),但这里有一个简单的例子,代码中有一些解释性注释,如果有任何完全不清楚的地方可以询问。

代码输出

\documentclass[border=5mm]{standalone}
\usepackage{pgfgantt} % for ganttcharts
\usepackage{etoolbox} % for \AtBeginEnvironment

% make a new counter for the line number in a chart
\newcounter{ganttline}
% set the counter to zero at start of every ganttchart environment
\AtBeginEnvironment{ganttchart}{\setcounter{ganttline}{0}}
\begin{document}

\begin{ganttchart}[
  % for each of bar, group, milestone:
  %   - step the line counter
  %   - print the current value of the counter followed by period
  %   - add text of item in the default manner at the end
  bar label text={\refstepcounter{ganttline}\arabic{ganttline}. \strut#1},
  group label text={\refstepcounter{ganttline}\arabic{ganttline}. \strut#1},
  milestone label text={\refstepcounter{ganttline}\arabic{ganttline}. \strut#1},
  % set text in a 5cm wide box, adjust 5cm to your liking
  bar label node/.append style={text width=5cm},
  group label node/.append style={text width=5cm},
  milestone label node/.append style={text width=5cm}
]{1}{12}
\gantttitle{2011}{12} \\
\gantttitlelist{1,...,12}{1} \\
\ganttgroup{Group 1}{1}{7} \\
\ganttbar{Task 1}{1}{2} \\
\ganttlinkedbar{Task 2}{3}{7} \ganttnewline
\ganttmilestone{Milestone}{7} \ganttnewline
\ganttbar{Final Task}{8}{12}
\ganttlink{elem2}{elem3}
\ganttlink{elem3}{elem4}
\end{ganttchart}
\end{document} 

有一些台词:

代码输出

\documentclass[border=5mm]{standalone}
\usepackage{pgfgantt} % for ganttcharts
\usepackage{etoolbox} % for \AtBeginEnvironment

% make a new counter for the line number in a chart
\newcounter{ganttline}
% set the counter to zero at start of every ganttchart environment
\AtBeginEnvironment{ganttchart}{\setcounter{ganttline}{0}}
\begin{document}

\begin{tikzpicture}
\begin{ganttchart}[
  % for each of bar, group, milestone:
  %   - step the line counter
  %   - print the current value of the counter followed by period
  %   - add text of item in the default manner at the end
  bar label text={\refstepcounter{ganttline}\arabic{ganttline}. \strut#1},
  group label text={\refstepcounter{ganttline}\arabic{ganttline}. \strut#1},
  milestone label text={\refstepcounter{ganttline}\arabic{ganttline}. \strut#1},
  % set text in a 5cm wide box, adjust 5cm to your liking
  % give the node a name that depends on the counter
  % the counter is stepped after the naming, so label0 is the first, label4 is the last
  bar label node/.append style={text width=5cm,name=label\arabic{ganttline}},
  group label node/.append style={text width=5cm,name=label\arabic{ganttline}},
  milestone label node/.append style={text width=5cm,name=label\arabic{ganttline}},
  % the canvas node makes the frame around the chart
  canvas/.append style={name=canvas},
]{1}{12}
\gantttitle{2011}{12} \\
\gantttitlelist{1,...,12}{1} \\
\ganttgroup{Group 1}{1}{7} \\
\ganttbar{Task 1}{1}{2} \\
\ganttlinkedbar{Task 2}{3}{7} \ganttnewline
\ganttmilestone{Milestone}{7} \ganttnewline
\ganttbar{Final Task}{8}{12}
\ganttlink{elem2}{elem3}
\ganttlink{elem3}{elem4}
\end{ganttchart}

% draw a vertical line relative to the top left corner of label0 and bottom left corner of label4
\draw ([xshift=1.3em]label0.north west) -- ([xshift=1.3em]label4.south west);

% draw horizontal lines between the nodes
\foreach [evaluate={\y=int(\x+1)}] \x in {0,...,3}
 {
   % define helper coordinate halfway between the left corners
   \path (label\x.south west) -- coordinate (m\x) (label\y.north west);
   % draw a line from that coordinate to the canvas box
   \draw (m\x) -- (m\x -| canvas.west);
 }
\end{tikzpicture}
\end{document} 

相关内容