[Tex/LaTex] How to make pgfgantt scale to specific widths in the page? (ex. textwidth)

chartspgfgantttikz-pgf

I'm trying to use pgfgantt to create a gantt chart for a project spanning many weeks, thus the following code would overflow through the right margin. I can't find a way to alter the size of the grid elements or time slots:

\begin{tikzpicture}[x=.5cm, y=1cm]
\begin{ganttchart}%
[vgrid, hgrid]{50} % 50 weeks
\gantttitle{Title}{12} \\
\ganttbar{Task 1}{1}{4} \\
\ganttbar{Task 2}{5}{6} \\
\ganttmilestone{M 1}{6} \\
\ganttbar{Task 3}{7}{11}
\ganttlink{4}{2}{5}{3}
\ganttlink[b-m]{6}{3}{6}{4}
\ganttlink[m-b]{6}{4}{7}{5}
\end{ganttchart}
\end{tikzpicture}

My former question above, however here's a new angle…

How can I make pgfgantt scale everything by limiting its size to specific parameters? ex. I want it to span the full textwidth of the page or the full width save for the margins.

Best Answer

With command \resizebox{new width}{new height}{what you want to resize} you can change dimensions of any latex box. This means that everything (text, lines, simbols, ...) will be adjusted to new dimensions. In first or second parameter you can use ! in order to keep original aspect ratio. With this command,

\documentclass{article}
\usepackage{lipsum}
\usepackage{pgfgantt}

\begin{document}
\lipsum[1]

\noindent\resizebox{\textwidth}{!}{
\begin{tikzpicture}[x=.5cm, y=1cm]
\begin{ganttchart}%
[vgrid, hgrid]{50} % 50 weeks
\gantttitle{Title}{12} \\    
\ganttbar{Task 1}{1}{4} \\    
\ganttbar{Task 2}{5}{6} \\    
\ganttmilestone{M 1}{6} \\    
\ganttbar{Task 3}{7}{11}
%\ganttlink{4}{2}{5}{3}
%\ganttlink[b-m]{6}{3}{6}{4}
%\ganttlink[m-b]{6}{4}{7}{5}
\end{ganttchart}
\end{tikzpicture}
}

\lipsum[2]
\end{document}

your Gantt chart would look something like:

enter image description here

Update: Solution for pgfgantt 4.0

As is stated in Missing number treated as zero in gantttitle, last version of pgfgantt is not completely backwards compatible. And now ganttchar needs two mandatory arguments

\begin{ganttchar}[options]{<start tss>}{<end tss>} 

Therefore, previous example should be:

\documentclass{article}
\usepackage{lipsum}
\usepackage{pgfgantt}

\begin{document}
\lipsum[1]

\noindent\resizebox{\textwidth}{!}{
\begin{tikzpicture}[x=.5cm, y=1cm]
\begin{ganttchart}[vgrid, hgrid]{1}{50} % 50 weeks
\gantttitle{Title}{12} \\   
\ganttbar{Task 1}{1}{4} \\    
\ganttbar{Task 2}{5}{6} \\    
\ganttmilestone{M 1}{6} \\    
\ganttbar{Task 3}{7}{11}
%\ganttlink{4}{2}{5}{3}
%\ganttlink[b-m]{6}{3}{6}{4}
%\ganttlink[m-b]{6}{4}{7}{5}
\end{ganttchart}
\end{tikzpicture}
}

\lipsum[2]
\end{document}
Related Question