1

I have a Gantt produced with pgfgantt package. However, when you try to center it centers the entire figure (including names of the tasks).

I would like to have the table centered, and then the names and other things floating relative to it.

I imagine that the tikzpicture has some type of anchor set in the center of the entire set of items to center the image. So, I was wondering if it is possible to shift that anchor to the center of the table, instead of the whole figure.

I went through the manual, but couldn't find information on this regard. I tried to show what I mean by "centering the table" in the figures below. Note that due to the crop I used to produce the image the margins are out, but imagine the margins around the caption of the image.

enter image description here

\documentclass{article}

\usepackage{pgfgantt}

\pagestyle{empty}

\begin{document}
\begin{figure}[h]
  \begin{tikzpicture}
  \begin{ganttchart}{1}{12}
  \ganttbar{Task with really long name}{1}{2} \\
  \end{ganttchart}
  \end{tikzpicture}
  \caption{Not centered on the table.}
\end{figure}

\begin{figure}[h]
  \hspace*{-40px}% manually adjusted
  \begin{tikzpicture}
  \begin{ganttchart}{1}{12}
  \ganttbar{Task with really long name}{1}{2} \\
  \end{ganttchart}
  \end{tikzpicture}
  \caption{Kind of centered on the table.}
\end{figure}

\end{document}
0

2 Answers 2

2

Here's a bit of a roundabout way. Put the whole ganttchart in a pgfinterruptboundingbox environment, which essentially means that it will not be considered when deciding the bounding box. Then add canvas/.append style={alias=frame} to the ganttchart options (see code below), and after the ganttchart environment set the bounding box explicitly with

\useasboundingbox (frame.south west) rectangle (frame.north east);

All of this needs to be inside a tikzpicture environment, though if you just have the ganttchart environment the surrounding tikzpicture is not required.

Also note that you don't want to have \\ after the last ganttbar, as that creates some additional whitespace.

enter image description here

\documentclass{article}

\usepackage{pgfgantt}

\pagestyle{empty}

\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{pgfinterruptboundingbox}
  \begin{ganttchart}[canvas/.append style={alias=frame}]{1}{12}
  \ganttbar{Task with really long name}{1}{2} 
  \end{ganttchart}
\end{pgfinterruptboundingbox}
\useasboundingbox (frame.south west) rectangle (frame.north east);
\end{tikzpicture}
  \caption{Not centered on the table.} % actually, it is
\end{figure}

\end{document}
1

Wrap the label into \llap{...} or \makebox[0pt][r]{...} to make it zero width and stick out to the left, and add \centering to your figure. The remaining 'uncenteredness' is probably due to the distance of the label to the box.

\documentclass{article}
\usepackage{pgfgantt}
\begin{document}
\begin{figure}
  \centering
  \begin{tikzpicture}
    \begin{ganttchart}{1}{12}
      \ganttbar{\llap{Task with really long name}}{1}{2} \\
%      \ganttbar{\makebox[0pt][r]{Task with really long name}}{1}{2} \\
    \end{ganttchart}
  \end{tikzpicture}
  \caption{Kind of centered on the table.}
\end{figure}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .