无法使用 chronology.sty 创建多个时间线

无法使用 chronology.sty 创建多个时间线

我想创建一个在一页上包含多个时间轴的文档。我正在使用年表.sty,使用 TikZ 来实现这一点。但是,当我尝试在文档中创建多个时间线时,出现错误。这是一个有效的块,它创建了一个只有一条时间线的文档:

\documentclass{article}
\usepackage{chronology}
\begin{document}
\begin{chronology}[2]{2000}{2012}{5ex}{\textwidth}
\event{2004}{single event}
\event[2007]{2009}{Multi-year event}
\end{chronology}
\end{document}

但现在我想在上面下方添加另一条时间线:

\documentclass{article}
\usepackage{chronology}
\begin{document}
\begin{chronology}[2]{2000}{2012}{5ex}{\textwidth}
\event{2004}{single event}
\event[2007]{2009}{Multi-year event}
\end{chronology}

\begin{chronology}[2]{2000}{2012}{5ex}{\textwidth}
\event{2002}{single event 2}
\event[2006]{2008}{Multi-year event 2}
\end{chronology}
\end{document}

这会产生错误:

! LaTeX Error: Command \c@step already defined

看起来问题是由 chronology.sty 创建的计数器变量引起的:

\newcounter{step}

每次我创建时间顺序环境时,LaTeX 都会尝试重新定义“步骤”吗?如果是这样,我该如何解决这个问题?

编辑: 我在这里复制代码时出错了。我的第一个 chronology 环境已正确关闭,但仍然出现错误。抱歉打错了。

答案1

包裹chronology定义了计数器、长度和盒子的数量之内环境chronology。这意味着这些长度/框在第一次使用\begin{chronology}...后已经存在\end{chronology},导致第二次使用时出错。从文件中删除有问题的\newcounter,\newlength\newsavebox定义可以解决此问题:\newenvironment{chronology}.sty

\newcounter{step}\newcounter{stepstart}\newcounter{stepstop}%
\newcounter{yearstart}\newcounter{yearstop}\newcounter{deltayears}%
\newlength{\xstart}\newlength{\xstop}%
\newlength{\unit}\newlength{\timelinewidth}%
\newsavebox{\timelinebox}%

现在,编译您的 MWE,它会生成正确的结果而没有任何错误。

\documentclass{article}
\usepackage{chronology}
\begin{document}
\begin{chronology}[2]{2000}{2012}{5ex}{\textwidth}
\event{2004}{single event}
\event[2007]{2009}{Multi-year event}
\end{chronology}% Inserted this missing environment \end

\begin{chronology}[2]{2000}{2012}{5ex}{\textwidth}
\event{2002}{single event 2}
\event[2006]{2008}{Multi-year event 2}
\end{chronology}
\end{document}

在此处输入图片描述

此错误不会对编译您的文档造成严重影响。您可以跳过(或忽略)这些错误,TeX 仍应生成所需的输出。但是,我建议联系软件包作者并请求更新以修改/更正此行为,因为它相当简单。某些命令遵守范围规则;在这种情况下,定义是全局的。为了完整起见,以下是新的、更新的chronology.sty

%%
%% This is file `chronology.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% chronology.dtx  (with options: `package')
%% 
%% This is a generated file.
%% 
%% Copyright (C) 2010 by Levi Wiseman
%% 
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%   http://www.latex-project.org/lppl.txt
%% and version 1.3 or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.
%% 
%% This work has the LPPL maintenance status `maintained'.
%% 
%% The Current Maintainer of this work is Levi Wiseman.
%% 
%% This work consists of the file chronology.dtx and the derived files
%% chronology.ins, chronology.pdf, and chronology.sty.
%% 
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{chronology}[2010/6/12 v1.0 Horizontal timeline]
\RequirePackage{calc}
\RequirePackage{tikz}
\RequirePackage{ifthen}

% MOVED NEW DEFINITIONS (COUNTERS/LENGTHS/BOXES)
  \newcounter{step}\newcounter{stepstart}\newcounter{stepstop}%
  \newcounter{yearstart}\newcounter{yearstop}\newcounter{deltayears}%
  \newlength{\xstart}\newlength{\xstop}%
  \newlength{\unit}\newlength{\timelinewidth}%
  \newsavebox{\timelinebox}%
\newenvironment{chronology}[5][5]{%
  %\newcounter{step}\newcounter{stepstart}\newcounter{stepstop}%
  %\newcounter{yearstart}\newcounter{yearstop}\newcounter{deltayears}%
  %\newlength{\xstart}\newlength{\xstop}%
  %\newlength{\unit}\newlength{\timelinewidth}%
  \setcounter{step}{#1}%
  \setcounter{yearstart}{#2}\setcounter{yearstop}{#3}%
  \setcounter{deltayears}{\theyearstop-\theyearstart}%
  \setlength{\unit}{#4}%
  \setlength{\timelinewidth}{#5}%
  \pgfmathsetcounter{stepstart}%
    {\theyearstart+\thestep-mod(\theyearstart,\thestep)}%
  \pgfmathsetcounter{stepstop}{\theyearstop-mod(\theyearstop,\thestep)}%
  \addtocounter{step}{\thestepstart}%
  %\newsavebox{\timelinebox}%
  \begin{lrbox}{\timelinebox}%
    \begin{tikzpicture}[baseline={(current bounding box.north)}]%
      \draw [|->] (0,0) -- (\thedeltayears*\unit+\unit, 0);%
      \foreach \x in {1,...,\thedeltayears}%
        \draw[xshift=\x*\unit] (0,-.1\unit) -- (0,.1\unit);%
      \addtocounter{deltayears}{1}%
      \foreach \x in {\thestepstart,\thestep,...,\thestepstop}{%
        \pgfmathsetlength\xstop{(\x-\theyearstart)*\unit}%
        \draw[xshift=\xstop] (0,-.3\unit) -- (0,.3\unit);%
        \node at (\xstop,0) [below=.2\unit] {\x};}}{%
    \end{tikzpicture}%
  \end{lrbox}%
  \raisebox{2ex}{\resizebox{\timelinewidth}{!}{\usebox{\timelinebox}}}}%
\newcommand{\event}[3][e]{%
  \pgfmathsetlength\xstop{(#2-\theyearstart)*\unit}%
  \ifx #1e%
    \draw[fill=black,draw=none,opacity=0.5]%
      (\xstop, 0) circle (.2\unit)%
      node[opacity=1,rotate=45,right=.5\unit] {#3};%
  \else%
    \pgfmathsetlength\xstart{(#1-\theyearstart)*\unit}%
    \draw[fill=black,draw=none,opacity=0.5,rounded corners=.2\unit]%
      (\xstart,-.2\unit) rectangle%
      node[opacity=1,rotate=45,right=.5\unit] {#3} (\xstop,.2\unit);%
  \fi}%
\newcommand{\decimaldate}[3]{(#1-1)/31/12+(#2-1)/12+#3}
\endinput
%%
%% End of file `chronology.sty'.

答案2

\end{chronology}每人都必须有一个\begin{chronology}

\documentclass{article}
\usepackage{chronology}
\begin{document}

\begin{chronology}[2]{2000}{2012}{5ex}{\textwidth}
\event{2004}{single event}
\event[2007]{2009}{Multi-year event}
\end{chronology}

\begin{chronology}[2]{2000}{2012}{5ex}{\textwidth}
\event{2002}{single event 2}
\event[2006]{2008}{Multi-year event 2}
\end{chronology}

\end{document}

我没有机会测试代码,但我认为它会起作用。

相关内容