![如何在时间线中包含日期?](https://linux22.com/image/362812/%E5%A6%82%E4%BD%95%E5%9C%A8%E6%97%B6%E9%97%B4%E7%BA%BF%E4%B8%AD%E5%8C%85%E5%90%AB%E6%97%A5%E6%9C%9F%EF%BC%9F.png)
当我编译以下代码时,我在短文档中获得了一个很好的时间线。但是,当我尝试将同一段代码编译成更长的文档(440 页的论文)时,我的时间线上没有出现具体日期,如下图所示:我发现,当我的文档中有两条时间线时,就会出现问题。只有一条时间线不会产生该错误。你能帮我解决这个问题吗?谢谢!
\documentclass[fontsize=12pt,%
twoside=semi,%
headings=small,%
chapterprefix=true,%
listof=flat]%
{scrbook}
\usepackage{chronosys}
%%%%%%%%%%%%%%%%%%%%TIMELINE%%%%%%%%%%%%
\usepackage{tikz}
\usetikzlibrary{arrows, calc, decorations.markings, positioning}
\makeatletter
\newenvironment{timeline}[6]{%
% #1 is startyear
% #2 is tlendyear
% #3 is yearcolumnwidth
% #4 is rulecolumnwidth
% #5 is entrycolumnwidth
% #6 is timelineheight
\newcommand{\startyear}{#1}
\newcommand{\tlendyear}{#2}
\newcommand{\yearcolumnwidth}{#3}
\newcommand{\rulecolumnwidth}{#4}
\newcommand{\entrycolumnwidth}{#5}
\newcommand{\timelineheight}{#6}
\newcommand{\templength}{}
\newcommand{\entrycounter}{0}
% https://tex.stackexchange.com/questions/85528/checking-whether-or-not-a-node-has-been-previously-defined
% https://tex.stackexchange.com/questions/37709/how-can-i-know-if-a-node-is-already-defined
\long\def\ifnodedefined##1##2##3{%
\@ifundefined{pgf@sh@ns@##1}{##3}{##2}%
}
\newcommand{\ifnodeundefined}[2]{%
\ifnodedefined{##1}{}{##2}
}
\newcommand{\drawtimeline}{%
\draw[timelinerule] (\yearcolumnwidth+5pt, 0pt) -- (\yearcolumnwidth+5pt, -\timelineheight);
\draw (\yearcolumnwidth+0pt, -10pt) -- (\yearcolumnwidth+10pt, -10pt);
\draw (\yearcolumnwidth+0pt, -\timelineheight+15pt) -- (\yearcolumnwidth+10pt, -\timelineheight+15pt);
\pgfmathsetlengthmacro{\templength}{neg(add(multiply(subtract(\startyear, \startyear), divide(subtract(\timelineheight, 25), subtract(\tlendyear, \startyear))), 10))}
\node[year] (year-\startyear) at (\yearcolumnwidth, \templength) {\startyear};
\pgfmathsetlengthmacro{\templength}{neg(add(multiply(subtract(\tlendyear, \startyear), divide(subtract(\timelineheight, 25), subtract(\tlendyear, \startyear))), 10))}
\node[year] (year-\tlendyear) at (\yearcolumnwidth, \templength) {\tlendyear};
}
\newcommand{\entry}[2]{%
% #1 is the year
% #2 is the entry text
\pgfmathtruncatemacro{\lastentrycount}{\entrycounter}
\pgfmathtruncatemacro{\entrycounter}{\entrycounter + 1}
\ifdim \lastentrycount pt > 0 pt%
\node[entry] (entry-\entrycounter) [below of=entry-\lastentrycount] {##2};
\else%
\pgfmathsetlengthmacro{\templength}{neg(add(multiply(subtract(\startyear, \startyear), divide(subtract(\timelineheight, 25), subtract(\tlendyear, \startyear))), 10))}
\node[entry] (entry-\entrycounter) at (\yearcolumnwidth+\rulecolumnwidth+10pt, \templength) {##2};
\fi
\ifnodeundefined{year-##1}{%
\pgfmathsetlengthmacro{\templength}{neg(add(multiply(subtract(##1, \startyear), divide(subtract(\timelineheight, 25), subtract(\tlendyear, \startyear))), 10))}
\draw (\yearcolumnwidth+2.5pt, \templength) -- (\yearcolumnwidth+7.5pt, \templength);
\node[year] (year-##1) at (\yearcolumnwidth, \templength) {##1};
}
\draw ($(year-##1.east)+(2.5pt, 0pt)$) -- ($(year-##1.east)+(7.5pt, 0pt)$) -- ($(entry-\entrycounter.west)-(5pt,0)$) -- (entry-\entrycounter.west);
}
\newcommand{\plainentry}[2]{% plainentry won't print date in the timeline
% #1 is the year
% #2 is the entry text
\pgfmathtruncatemacro{\lastentrycount}{\entrycounter}
\pgfmathtruncatemacro{\entrycounter}{\entrycounter + 1}
\ifdim \lastentrycount pt > 0 pt%
\node[entry] (entry-\entrycounter) [below of=entry-\lastentrycount] {##2};
\else%
\pgfmathsetlengthmacro{\templength}{neg(add(multiply(subtract(\startyear, \startyear), divide(subtract(\timelineheight, 25), subtract(\tlendyear, \startyear))), 10))}
\node[entry] (entry-\entrycounter) at (\yearcolumnwidth+\rulecolumnwidth+10pt, \templength) {##2};
\fi
\ifnodeundefined{invisible-year-##1}{%
\pgfmathsetlengthmacro{\templength}{neg(add(multiply(subtract(##1, \startyear), divide(subtract(\timelineheight, 25), subtract(\tlendyear, \startyear))), 10))}
\draw (\yearcolumnwidth+2.5pt, \templength) -- (\yearcolumnwidth+7.5pt, \templength);
\node[year] (invisible-year-##1) at (\yearcolumnwidth, \templength) {};
}
\draw ($(invisible-year-##1.east)+(2.5pt, 0pt)$) -- ($(invisible-year-##1.east)+(7.5pt, 0pt)$) -- ($(entry-\entrycounter.west)-(5pt,0)$) -- (entry-\entrycounter.west);
}
\begin{tikzpicture}
\tikzstyle{entry} = [%
align=left,%
text width=\entrycolumnwidth,%
node distance=10mm,%
anchor=west]
\tikzstyle{year} = [anchor=east]
\tikzstyle{timelinerule} = [%
draw,%
decoration={markings, mark=at position 1 with {\arrow[scale=1.5]{latex'}}},%
postaction={decorate},%
shorten >=0.4pt]
\drawtimeline
}
{
\end{tikzpicture}
\let\startyear\@undefined
\let\tlendyear\@undefined
\let\yearcolumnwidth\@undefined
\let\rulecolumnwidth\@undefined
\let\entrycolumnwidth\@undefined
\let\timelineheight\@undefined
\let\entrycounter\@undefined
\let\ifnodedefined\@undefined
\let\ifnodeundefined\@undefined
\let\drawtimeline\@undefined
\let\entry\@undefined
}
\makeatother
%%%%%%%%%%%%%%%%%FIN TIMELINE%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\pagestyle{headings}
\begin{table}[!]
\begin{timeline}{2014}{2017}{2cm}{2.5cm}{10cm}{5cm}
\entry{2014}{Octobre. Enregistrement}
\entry{2014}{Novembre. Transcription}
\entry{2015}{Mars. Travail}
\entry{2016}{Avril. Reprise}
\entry{2017}{Septembre. Fin}
\end{timeline}
\caption{Chronologie}
\end{table}
\begin{table}[!]
\begin{timeline}{2014}{2017}{2cm}{2.5cm}{10cm}{5cm}
\entry{2014}{Octobre. Enregistrement}
\entry{2014}{Novembre. Transcription}
\entry{2015}{Mars. Travail}
\entry{2016}{Avril. Reprise}
\entry{2017}{Septembre. Fin}
\end{timeline}
\caption{Chronologie}
\end{table}
\end{document}
答案1
这里发布的代码对我来说无法编译,而且\timeline
宏与原始源代码不同,原始源代码在此处:如何创建垂直时间线?我没有尝试找出哪里不同。
我用原始来源的代码替换了发布的时间线代码,并提供了两个示例。第一个示例显示每个时间线条目的年份。这可以使用宏来完成\entry
。第二个示例显示了时间线条目的添加,但不显示年份。这是通过\plainentry
宏完成的。
结果如下:
这是 MWE:
\documentclass[fontsize=12pt,%
twoside=semi,%
headings=small,%
chapterprefix=true,%
listof=flat]%
{scrbook}
%\usepackage{chronosys}
\usepackage{verbatim}
%%%%%%%%%%%%%%%%%%%%TIMELINE%%%%%%%%%%%%
\usepackage{tikz}
\usetikzlibrary{arrows, calc, decorations.markings, positioning}
% https://tex.stackexchange.com/a/197140
\makeatletter
\newenvironment{timeline}[6]
{
% #1 is startyear
% #2 is tlendyear
% #3 is yearcolumnwidth
% #4 is rulecolumnwidth
% #5 is entrycolumnwidth
% #6 is timelineheight
\newcommand{\startyear}{#1}
\newcommand{\tlendyear}{#2}
\newcommand{\yearcolumnwidth}{#3}
\newcommand{\rulecolumnwidth}{#4}
\newcommand{\entrycolumnwidth}{#5}
\newcommand{\timelineheight}{#6}
\newcommand{\templength}{}
\newcommand{\entrycounter}{0}
% http://tex.stackexchange.com/questions/85528/checking-whether-or-not-a-node-has-been-previously-defined
% http://tex.stackexchange.com/questions/37709/how-can-i-know-if-a-node-is-already-defined
\long\def\ifnodedefined##1##2##3{%
\@ifundefined{pgf@sh@ns@##1}{##3}{##2}%
}
\newcommand{\ifnodeundefined}[2]{%
\ifnodedefined{##1}{}{##2}
}
\newcommand{\drawtimeline}{%
\draw[timelinerule] (\yearcolumnwidth+5pt, 0pt) -- (\yearcolumnwidth+5pt, -\timelineheight);
\draw (\yearcolumnwidth+0pt, -10pt) -- (\yearcolumnwidth+10pt, -10pt);
\draw (\yearcolumnwidth+0pt, -\timelineheight+15pt) -- (\yearcolumnwidth+10pt, -\timelineheight+15pt);
\pgfmathsetlengthmacro{\templength}{neg(add(multiply(subtract(\startyear, \startyear), divide(subtract(\timelineheight, 25), subtract(\tlendyear, \startyear))), 10))}
\node[year] (year-\startyear) at (\yearcolumnwidth, \templength) {\startyear};
\pgfmathsetlengthmacro{\templength}{neg(add(multiply(subtract(\tlendyear, \startyear), divide(subtract(\timelineheight, 25), subtract(\tlendyear, \startyear))), 10))}
\node[year] (year-\tlendyear) at (\yearcolumnwidth, \templength) {\tlendyear};
}
\newcommand{\entry}[2]{%
% #1 is the year
% #2 is the entry text
\pgfmathtruncatemacro{\lastentrycount}{\entrycounter}
\pgfmathtruncatemacro{\entrycounter}{\entrycounter + 1}
\ifdim \lastentrycount pt > 0 pt%
\node[entry] (entry-\entrycounter) [below of=entry-\lastentrycount] {##2};
\else%
\pgfmathsetlengthmacro{\templength}{neg(add(multiply(subtract(\startyear, \startyear), divide(subtract(\timelineheight, 25), subtract(\tlendyear, \startyear))), 10))}
\node[entry] (entry-\entrycounter) at (\yearcolumnwidth+\rulecolumnwidth+10pt, \templength) {##2};
\fi
\ifnodeundefined{year-##1}{%
\pgfmathsetlengthmacro{\templength}{neg(add(multiply(subtract(##1, \startyear), divide(subtract(\timelineheight, 25), subtract(\tlendyear, \startyear))), 10))}
\draw (\yearcolumnwidth+2.5pt, \templength) -- (\yearcolumnwidth+7.5pt, \templength);
\node[year] (year-##1) at (\yearcolumnwidth, \templength) {##1};
}
\draw ($(year-##1.east)+(2.5pt, 0pt)$) -- ($(year-##1.east)+(7.5pt, 0pt)$) -- ($(entry-\entrycounter.west)-(5pt,0)$) -- (entry-\entrycounter.west);
}
\newcommand{\plainentry}[2]{% plainentry won't print date in the timeline
% #1 is the year
% #2 is the entry text
\pgfmathtruncatemacro{\lastentrycount}{\entrycounter}
\pgfmathtruncatemacro{\entrycounter}{\entrycounter + 1}
\ifdim \lastentrycount pt > 0 pt%
\node[entry] (entry-\entrycounter) [below of=entry-\lastentrycount] {##2};
\else%
\pgfmathsetlengthmacro{\templength}{neg(add(multiply(subtract(\startyear, \startyear), divide(subtract(\timelineheight, 25), subtract(\tlendyear, \startyear))), 10))}
\node[entry] (entry-\entrycounter) at (\yearcolumnwidth+\rulecolumnwidth+10pt, \templength) {##2};
\fi
\ifnodeundefined{invisible-year-##1}{%
\pgfmathsetlengthmacro{\templength}{neg(add(multiply(subtract(##1, \startyear), divide(subtract(\timelineheight, 25), subtract(\tlendyear, \startyear))), 10))}
\draw (\yearcolumnwidth+2.5pt, \templength) -- (\yearcolumnwidth+7.5pt, \templength);
\node[year] (invisible-year-##1) at (\yearcolumnwidth, \templength) {};
}
\draw ($(invisible-year-##1.east)+(2.5pt, 0pt)$) -- ($(invisible-year-##1.east)+(7.5pt, 0pt)$) -- ($(entry-\entrycounter.west)-(5pt,0)$) -- (entry-\entrycounter.west);
}
\begin{tikzpicture}
\tikzstyle{entry} = [%
align=left,%
text width=\entrycolumnwidth,%
node distance=11mm,%
anchor=west]
\tikzstyle{year} = [anchor=east]
\tikzstyle{timelinerule} = [%
draw,%
decoration={markings, mark=at position 1 with {\arrow[scale=1.5]{latex'}}},%
postaction={decorate},%
shorten >=0.4pt]
\drawtimeline
}
{
\end{tikzpicture}
\let\startyear\@undefined
\let\tlendyear\@undefined
\let\yearcolumnwidth\@undefined
\let\rulecolumnwidth\@undefined
\let\entrycolumnwidth\@undefined
\let\timelineheight\@undefined
\let\entrycounter\@undefined
\let\ifnodedefined\@undefined
\let\ifnodeundefined\@undefined
\let\drawtimeline\@undefined
\let\entry\@undefined
}
\makeatother
%%%%%%%%%%%%%%%%%FIN TIMELINE%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\pagestyle{headings}
%\begin{table}[!]
Using \verb|\entry| only
\begin{timeline}{2014}{2017}{2cm}{2.5cm}{10cm}{5cm}
\entry{2014}{Octobre. Enregistrement}
\entry{2014}{Novembre. Transcription}
\entry{2015}{Mars. Travail}
\entry{2016}{Avril. Reprise}
\entry{2017}{Septembre. Fin}
\end{timeline}
\bigskip
Using \verb|\plainentry| to blank the years for some timeline entries
\bigskip
\begin{timeline}{2014}{2017}{2cm}{2.5cm}{10cm}{5cm}
\entry{2014}{Octobre. Enregistrement}
\entry{2014}{Novembre. Transcription}
\plainentry{2015}{Mars. Travail}
\plainentry{2016}{Avril. Reprise}
\entry{2017}{Septembre. Fin}
\end{timeline}
%\caption{Chronologie}
%\end{table}
\end{document}