我希望幾乎每個甘特圖都適合自動地到頁面的文字寬度。這個問題中最重要的字是自動的。這就是它與其他類似問題的不同之處。
這文字寬度和時隙數圖表中沒有具體說明。該解決方案應該足夠靈活來處理它。
也許有一個解決方案計算 x unit
?下面的MWE有122個時隙。所以我會設定x unit
為\textwidth divided by 122
.有沒有辦法做到這一點?
\documentclass{article}
\usepackage[showframe]{geometry} % show page borders
\usepackage{pgfgantt}
\begin{document}
\noindent
\begin{ganttchart}
[
x unit=1.23mm, % timeslot width
time slot format=isodate % Datumsformat
]
{2017-04-01}{2017-07-31} % from-to (122 days)
\gantttitle{Gantt-Chart with 122 timeslots}{122}\\
\ganttbar{Short}{2017-05-01}{2017-06-01}\\
\ganttbar{Long Long}{2017-05-25}{2017-06-04}\\
\end{ganttchart}
\end{document}
答案1
經過聊天中的討論,這是艾倫答案的改編,它實現了他試圖實現的語法。
% addaswyd o ateb Alan Munn: https://tex.stackexchange.com/questions/364944/expand-a-gantt-chart-automatically-to-textwidth-of-a-page/364969#364969 a chwestiwn buhtz: https://tex.stackexchange.com/questions/364944/expand-a-gantt-chart-automatically-to-textwidth-of-a-page
\documentclass[a4paper]{article}
\usepackage[showframe]{geometry} % show page borders
\parindent=0pt
\usepackage{pgfgantt}
\makeatletter
\tikzset{%
/pgfgantt/time slots/.code={%
\tikzset{%
/pgfgantt/time slots/.cd,
#1,
/pgfgantt/.cd,
}%
},
/pgfgantt/time slots/.search also={/pgfgantt},
/pgfgantt/time slots/.cd,
width/.store in=\ts@width,
width=\textwidth,
slots/.store in=\totaltimeslots,
slots=20,
label width/.store in=\ts@labelwidth,
label width=25mm,
calc x unit/.code n args=3{%
\pgfmathsetmacro\ts@xunit{(#1-#2-0.6667em-2*\pgflinewidth)/#3}%
\tikzset{%
/pgfgantt/x unit=\ts@xunit pt,
}%
},
widest/.code={%
\pgfmathsetmacro\ts@wdlabel{width("#1")}%
\tikzset{/pgfgantt/time slots/label width=\ts@wdlabel pt}%
},
calc x unit aux width/.style={/pgfgantt/time slots/calc x unit={#1}{\ts@labelwidth}{\totaltimeslots}},
calc x unit aux label width/.style={/pgfgantt/time slots/calc x unit={\ts@width}{#1}{\totaltimeslots}},
calc x unit aux slots/.style={/pgfgantt/time slots/calc x unit={\ts@width}{\ts@labelwidth}{#1}},
width/.forward to=/pgfgantt/time slots/calc x unit aux width,
slots/.forward to=/pgfgantt/time slots/calc x unit aux slots,
label width/.forward to=/pgfgantt/time slots/calc x unit aux label width,
}
\makeatother
\begin{document}
\begin{ganttchart}
[
time slots={slots=122, widest=Long Long},
time slot format=isodate,
]
{2017-04-01}{2017-07-31} % from-to (122 days)
\gantttitle{Gantt-Chart with 122 timeslots}{\totaltimeslots}\\
\ganttbar{Short}{2017-05-01}{2017-06-01}\\
\ganttbar{Long Long}{2017-05-25}{2017-06-04}\\
\end{ganttchart}
\begin{ganttchart}
[
time slots/slots=15,
time slots/widest=Foo,
time slots/width=.9\linewidth,
time slot format=isodate,
]
{2017-04-01}{2017-04-15} % from-to (122 days)
\gantttitle{Gantt-Chart with 15 timeslots}{15}\\
\ganttbar{Foo}{2017-05-25}{2017-06-04}
\end{ganttchart}
\end{document}
答案2
使用calc
包裝根據需要設定長度。由於需要在命令中手動設定時隙數\ganttitle
,因此我創建了一個ganttchart
樣式timeslots
,該樣式將圖表的時隙數和最長\gantttitle
標籤的文字(逗號分隔)作為參數,然後使用它來計算寬度。它還設定\timeslots
可以在命令中使用的巨集的值\gantttitle
。
我已將程式碼新增為ganttchart
樣式,以便可以像以前的版本一樣在沒有自己的巨集的情況下指定它(有關詳細信息,請參閱編輯歷史記錄)。我還添加了您希望自動調整大小相對於的寬度的長度。對於您的具體範例,它設定為\textwidth
,但這允許您將其設定為您喜歡的任何寬度。
用法:[ ..., timeslots={<number>,<text of longest label>}, ... ]
\usepackage[]{geometry} % show page borders
\parindent=0pt
\usepackage{pgfgantt}
\usepackage{calc}
\newlength{\myunitx}
\newlength{\autosizewidth}
\setlength{\autosizewidth}{\textwidth} % this allows you to change the value for autosizing
\newcommand*{\timeslots}{}
\ganttset{
timeslots/.code args={#1,#2}{\setlength{\myunitx}{(\autosizewidth-\widthof{#2}-1em)/#1}
\renewcommand{\timeslots}{#1}\pgfkeys{pgfgantt/x unit=\myunitx}}
}
\begin{document}
\begin{ganttchart}
[
timeslots={122,Long Long}, % timeslots
time slot format=isodate % Datumsformat
]
{2017-04-01}{2017-07-31} % from-to (122 days)
\gantttitle{Gantt-Chart with 122 timeslots}{\timeslots}\\
\ganttbar{Short}{2017-05-01}{2017-06-01}\\
\ganttbar{Long Long}{2017-05-25}{2017-06-04}\\
\end{ganttchart}
\begin{ganttchart}
[
timeslots={15,Foo}, % timeslots
time slot format=isodate % Datumsformat
]
{2017-04-01}{2017-04-15} % from-to (122 days)
\gantttitle{Gantt-Chart with 15 timeslots}{15}\\
\ganttbar{Foo}{2017-05-25}{2017-06-04}
\end{ganttchart}
\end{document}