目標

目標

目標

私の目標は、複数の独立した文書に連続したページ (およびセクション) カウンターを用意することです。目的は、各論文がジャーナルのレイアウトを可能な限り維持しながらも、学校側が連続したページ付けを要求する累積的な論文を作成することです。複数の論文を貼り合わせたような醜い見た目になる可能性は高いですが、基本的にはそういうことです。

おそらく、ジャーナルの.cls-files からフォーマット情報を抽出し、環境にパックするのがクリーンな方法であることはわかっていますが、私のように平易な読み書きができない人にとっては、それは大変な作業になるでしょうTeX。そのため、私は別のアプローチ、つまりパッケージを使用したハックを試し、ある程度成功しましたxr。しかし、いくつかの問題が残っています。

まず私が何をしてきたか説明させてください:

私のセットアップ

序論と結論を含むメイン ファイルがあります。そのページ カウンタとセクション カウンタは両方とも 1 から始まる必要があるため、本文の最後の章から取り込まれた値で更新するのは、区切りの時点でのみにする必要があります。ドキュメント内で を使用しようとすると問題が発生します。これは、の出力を整数ではなく文字列として\setcounter{page}{\pageref{external-endfile}}表現することと関係しているようで、パッケージによってトリガーされているようです。そのため、数値を に入力し、必要に応じて実際のカウンタに読み込みます。このメイン ファイルは次のようになります。\pagerefbabel\newcounter

\documentclass{article}
\usepackage{xr} %imports labels from external document
  \externaldocument[finalCh]{file1} %prefix external labels to avoid name clashes
  \newcounter{finalcount}
  \setcounter{finalcount}{\pageref{finalChendfile}}% probably not the most elegant way to pack the input file's page number into a numberical variable, but it works.
  \stepcounter{finalcount}% +1 since counters are initialised at 0

  \newcounter{othersectioncount}
  \setcounter{othersectioncount}{\ref{finalChendfile}}
\usepackage{ifthen} %conditionals
\newcommand{\exportcounters}{
    \ifthenelse{%
              \isodd{\thepage}} % if current page is odd-numbered
              {\newpage \ % new page, forced space to make sure the following command is actually parsed, i.e. the page not treated as totally empty
              }%
              {\relax}
    \label{endfile}
    \newpage
}

%... other packages
\usepackage[english]{babel} %for some reason I don't understand, this clashes with treating \pageref as a number, so you have to put it *after* determining the page number
\usepackage{lipsum}

\begin{document}

\tableofcontents
\section{Introduction}
\subsection{Subsection}
\lipsum[1-2]
\newpage
    \subsection{Another subsection}
\exportcounters

\setcounter{section}{\theothersectioncount}
\section{Conclusions}
\setcounter{page}{\thefinalcount} %load the final pagenumber of the last chapter here

\lipsum[4-40]


\end{document}

それ以外にも、この章のファイルがあり、次のような内容になっています。

\documentclass{article}
\usepackage{substr}
\usepackage{xr} %imports labels from external document
\usepackage{ifthen} %conditionals
  \externaldocument[intro]{introandextro} %prefix external labels to avoid name clashes
  \addtocounter{page}{\pageref{introendfile}}
\newcommand{\exportcounters}{
    \ifthenelse{%
              \isodd{\thepage}} % if current page is odd-numbered
              {\newpage \ % new page, forced space to make sure the following command is actually parsed, i.e. the page not treated as totally empty
              }%
              {\relax}
    \label{endfile}
    \newpage
}

\usepackage{lipsum}

%\setcounter{section}{2}
\begin{document}

\setcounter{section}{\ref{introendfile}}
\section{Chapter I}
\subsection{intro}
\lipsum[1-4]
\newpage
\subsection{Chapter 1 body}
\lipsum[5-44]

\exportcounters
\label{endfile}


\end{document}

この設定では、連続したページ番号とセクション番号を取得するために、「pdflatex introandextro」、「pdflatex file1」、そしてもう一度「pdflatex introandextro」をそれぞれ 2 回実行するだけで済みます (これを行うためのシェル スクリプトがあります)。また、tocさまざまなファイルからすべての関連情報を抽出しaux、それを使用してメイン ファイルをコンパイルする Python スクリプトもあります。これにより、tocどのファイルに出現するかに関係なく、すべてのセクションを参照する目次が作成され、ほぼ期待どおりに機能します。

当然のことながら、\addtocounter{page}{\pageref{introendfile}}導入部の最後のページカウンタを file1 にインポートします。また、当然のことながら、\setcounter{section}{\ref{introendfile}}外部参照の戻り値 (この場合は 1.2) のセクション部分を取り込み、それを適切に使用してセクションカウンタを設定します。残念ながら、式の残りの部分をどうすればよいかわからないため、再度吐き出し、ページの上部に ".2" と書き込みます。これは見苦しいので、これを削除しようとしています。私の直感では、これに対する最良の方法は、最上位のセクション コマンドのみを と に送信するように一時的に を再定義することだと思いますが、\label私のプレーン tex ではどこから始めればよいかわかりません。\exportcounters.aux.toc

残存問題

そこで質問です。-command を適切に再定義する方法を誰か教えてくれませんか? 言い換えると、サブセクションまたはより深いセクションに埋め込まれているにもかかわらず、-fileではなく-command\labelに伝えるにはどうすればよいですか? (または、 に渡す前に '.' 以降の部分を削除するにはどうすればよいですか? 試してみましたが、数値ではなく文字列が生成され、コンパイル時に「数値が欠落しているため、ゼロとして扱われます」というエラーが発生します。).aux\newlabel{endfile}{{<thesection>}{12}}\newlabel{endfile}{{<\thesection>.<\thesubsection>}{12}}\setcounter{section}{...}\SubStringBefore

2 つ目の問題は、両方のドキュメントの最初のページを終了する場所に明示的に を挿入する必要があることです。そうしないと、2 ページ分のテキストが実行されてしまいます。この動作は、 (またはとの結合)\newpageによってトリガーされるようです。これは既知の問題ですか? また、明示的な を必要としない回避策はありますか?xrxrlipsum\newpage

もう 1 つの (非常に小さな) 問題は、pdflatex がプリアンブルでの の使用についてエラーを出力\ref("Missing \begin{document}") することですが、結果が正しい場合はこれを無視しても問題ありません。

あるいは、まったく異なる角度からこの問題に取り組むことは可能でしょうか? プレーンな TeX をあまり理解していない人が、.cls環境に書式設定コマンドを詰め込むのを支援するツールはありますか? のようなものですか\importasenvironment{<environmentname>}{<classname>.cls}?

答え1

以前のドキュメントからページとセクションの値をインポートするには、パッケージzref、モジュールlastpage、を使用しますxr

最初の文書

最初のドキュメントは、パッケージによって設定されたラベルによって書き込まれるプロパティ リストにDocumentAプロパティpagevalueとを保存します。sectionvalueLastPageLastPagezref-lastpage

サンプルファイルDocumentA.tex、最初のドキュメント:

\documentclass{article}

\usepackage{zref-lastpage}
\makeatletter
\zref@ifpropundefined{pagevalue}{% it is defined by some modules of zref
  \zref@newprop*{pagevalue}[0]{\number\value{page}}%
}{}
\zref@newprop{sectionvalue}[0]{\number\value{section}}%
\zref@addprops{LastPage}{pagevalue,sectionvalue}
\makeatother

\begin{document}
\section{Section A of first document}
\section{Section B of first document}
\newpage
\section{Section C of first document}
\section{Section D of first document}
\end{document}

パッケージはzref-lastpage機能します:

  • への行き方本物ドキュメントの末尾 (パッケージ経由atveryend)。
  • . を使用.auxしてファイルに書き込むと\immediate\write、ページ番号が正しくなりますが、これには次のページを出力する必要があります。最後のページの後には、そのようなページはありません。また、パッケージはページ番号の修正も行います。\label\write

ファイル.auxには次の内容が含まれます。

\zref@newlabel{LastPage}{\default{4}\page{2}\abspage{2}\pagevalue{2}\sectionvalue{4}}

次の文書

次のドキュメントは、前のドキュメントのラベルからデータをインポートしLastPage、カウンターを設定します。また、前のドキュメントのコードを使用して、プロパティを定義し、次のドキュメントのカウンター値を書き込みます。

サンプルファイルDocumentB.tex、次のドキュメント:

\documentclass{article}

\usepackage{zref-lastpage,zref-xr}

\makeatletter
\zref@ifpropundefined{pagevalue}{% it is defined by some modules of zref
  \zref@newprop*{pagevalue}[0]{\number\value{page}}%
}{}
\zref@newprop{sectionvalue}[0]{\number\value{section}}%
\zref@addprops{LastPage}{pagevalue,sectionvalue}

\zexternaldocument[pre-]{DocumentA}\relax
\zref@ifrefundefined{pre-LastPage}{%
  \@latex@error{Zref label `LastPage' of `DocumentA' is undefined}\@ehc
}{%
  \setcounter{page}{%
    \numexpr\zref@extractdefault{pre-LastPage}{pagevalue}{0}+1\relax
  }%
  \setcounter{section}{%
    \zref@extractdefault{pre-LastPage}{sectionvalue}{0}%
  }%
}
\makeatother

\begin{document}
\section{Section E of second document}
\section{Section F of second document}
\newpage
\section{Section G of second document}
\section{Section H of second document}
\end{document}

次に、2 番目のドキュメントの最初のセクションにSection E of second document5 という番号が付けられ、3 ページ目から始まります。

答え2

Heiko Oberdiek の回答は、互いに完璧に構築されている複数のドキュメントの基本ケースを解決します。

組み込む章の前後の両方の部分を含むメイン ファイルという、やや複雑なケースでは、つまり、ドキュメント内の任意の時点でそのカウンターを外部ドキュメントに渡し、その後、ドキュメント内で同じまたは別の外部ドキュメントのカウンターを取得する場合、コマンドを定義して\importcounters適切\exportcountersな場所で呼び出すことで微調整を行いました。おそらくさらに削減できると思いますが、Heiko Oberdiek の投稿を基にして、現時点ではこれが私が実現できる範囲です。

\documentclass{article}

\usepackage{zref-lastpage,zref-xr}

\makeatletter
\zref@ifpropundefined{pagevalue}{% it is defined by some modules of zref
  \zref@newprop*{pagevalue}[0]{\number\value{page}}%
}{}
\zref@newprop{sectionvalue}[0]{\number\value{section}}%
%\zref@addprops{LastPage}{pagevalue,sectionvalue}%


\zexternaldocument[pre-]{DocumentB}\relax

\zref@ifrefundefined{pre-LastPage}{%
  \@latex@error{Zref label `LastPage' of `DocumentB' is undefined}\@ehc
}{%
  \newcounter{@otherpage}
  \setcounter{@otherpage}{%
    \numexpr\zref@extractdefault{pre-LastPage}{pagevalue}{0}+1\relax
  }%
  \newcounter{@othersection}
  \setcounter{@othersection}{%
    \zref@extractdefault{pre-LastPage}{sectionvalue}{0}%
  }%
}

\newcommand{\importcounters}{% enables importing the external page and section counters at an arbitrary point in the document
    \setcounter{section}{\the@othersection}
    \setcounter{page}{\the@otherpage}
}

\newcommand{\exportcounters}{% exporting counters after the intro rather than at the end of the document
    \cleardoublepage \ %
    \zref@labelbyprops{LastIntroPage}{pagevalue,sectionvalue}
}
\makeatother

\usepackage[english]{babel}
\begin{document}
\section{Section A of first document}
\section{Section B of first document}

\exportcounters
\newpage
\importcounters

\section{Section C of first document}
\section{Section D of first document}
\subsection{D 1}
\subsection{D 2}
\end{document}

( では、唯一の変更点は、ではなく にDocumentB.texカウンターを供給することです。)pre-LastIntroPagepre-LastPage

write18私の次の目標は、メイン ファイルを一時停止し、依存ファイルのコンパイルに移行してから先に進むために を使用する方法を見つけることですが、これは別の質問です。

関連情報