我的主文档包含在另一个文档中,该文档重新排列并添加了裁切标记。
.toc
为了使我的脚本通用,我需要从- 或- 文件中提取页面和部分计数.aux
。
以下摘自.toc
:
\contentsline {chapter}{XXX}{139}{section*.96}
\contentsline {part}{\bfseries \leavevmode {\color {gray}\Ordinalstringnum {9}~Teil:\hspace {0.5em}XXX}}{135}{part.9}
和.aux
:
\@writefile{toc}{\contentsline {chapter}{XXX}{139}{section*.96}}
\@writefile{toc}{\contentsline {part}{\bfseries \leavevmode {\color {gray}\Ordinalstringnum {9}~Teil:\hspace {0.5em}XXX}}{135}{part.9}}
当最后一页没有新的章节、部分等时,有没有什么方法可以获取页数?
梅威瑟:
\documentclass[twoside,landscape]{scrreprt}
\usepackage{forloop}
\usepackage{pdfpages}
\usepackage{background}
\usetikzlibrary{calc}
\usepackage[a4paper]{geometry}
\geometry{top=6mm, left=6mm, right=0mm, bottom=0mm}
\backgroundsetup{
scale=1,
opacity=1,
angle=0,
color=black,
contents={%
\begin{tikzpicture}[remember picture,overlay]
\draw[thick] ($(current page.north west)+(0mm,-9mm)$) -- ++(\paperwidth,0mm); %o
\draw[thick] ($(current page.south west)+(0mm,9mm)$) -- ++(\paperwidth,0mm); %u
\draw[thick] ($(current page.north east)+(-9mm,0mm)$) -- ++(0,-\paperheight); %r
\draw[thick] ($(current page.north west)+(9mm,0mm)$) -- ++(0,-\paperheight); %l
\draw[thick] ($(current page.north)+(0mm,0mm)$) -- ++(0,-\paperheight); %m
\end{tikzpicture}%
}}
\newcommand{\ing}[1]{\includegraphics[width=(\textwidth-6mm)/2,height=\textheight-6mm,page=#1]{empty}}
\newcommand{\pdf}[4]{
\hspace*{-11pt}%
\ing{\numexpr#1\relax}%
\ing{\numexpr#2\relax}%
\clearpage%
\hspace*{6pt}%
\ing{\numexpr#3\relax}%
\ing{\numexpr#4\relax}%
\clearpage%
}
\pagestyle{plain}
\newcounter{i}
\newcommand\maxpage{???}
\newcommand\emptypage{3}
\begin{document}
%%% Cover
\pdf{1}{3}{3}{3}
%%% Inhalt
\forloop{i}{1}{\value{i} < 34}{%
\pdf{\thei*4+2}{\thei*4-1}{\thei*4}{\thei*4+1}
}
%%% letzte 3 Seiten
\pdf{3}{137}{138}{139}
\null\clearpage
\end{document}
我需要它来完成以下几行:
\forloop{i}{1}{\value{i} < 34}{%
对于这一行我需要类似的东西\floor{\maxpage/4}
而不是34
\pdf{3}{137}{138}{139}
此行添加了最后 3 页。如果我将它们包含在循环中,则会收到错误,因为页面140
不存在。包含 -condition 的宏if
会很棒 ( if #1 > \maxpage then \emptypage
)
编辑:
A.pdf
是我的主要文件。B.tex
包括 的页面A.pdf
,重新排列它们并添加裁切标记以供打印。最终在印刷机上印刷的结果是B.pdf
。
我上面发布的代码来自B.tex
。这里我需要的页数,A.pdf
因此我需要一个命令来从辅助文件中提取页数A.aux
或A.toc
在中使用它B.tex
。希望这有助于澄清我的问题。
答案1
诀窍是明确地将.aux
页面标签或其他计数器值写入文件作为page
,这样\pageref{labelname}
将产生该值(因此也产生计数器值)
这需要运行 latex/pdflatex 两次甚至三次。
我添加了对part
计数的支持,处理总零件编号,如果零件编号在某个地方重置,也处理页码,通过引入Total....
计数器,这些计数器不会添加到重置列表中TeX
。
对于外部引用,需要使用xr
包,并在序言中明确说明要使用哪些文档。
读取零件编号.tex 引用其他文件的驱动程序
\documentclass[twoside,landscape]{scrreprt}
\usepackage{etoolbox}
\usepackage{everypage}
\usepackage{forloop}
\usepackage{pdfpages}
\usepackage{background}
\usetikzlibrary{calc}
\usepackage[a4paper]{geometry}
\geometry{top=6mm, left=6mm, right=0mm, bottom=0mm}
\usepackage{xr}
\backgroundsetup{
scale=1,
opacity=1,
angle=0,
color=black,
contents={%
\begin{tikzpicture}[remember picture,overlay]
\draw[thick] ($(current page.north west)+(0mm,-9mm)$) -- ++(\paperwidth,0mm); %o
\draw[thick] ($(current page.south west)+(0mm,9mm)$) -- ++(\paperwidth,0mm); %u
\draw[thick] ($(current page.north east)+(-9mm,0mm)$) -- ++(0,-\paperheight); %r
\draw[thick] ($(current page.north west)+(9mm,0mm)$) -- ++(0,-\paperheight); %l
\draw[thick] ($(current page.north)+(0mm,0mm)$) -- ++(0,-\paperheight); %m
\end{tikzpicture}%
}}
%\newcommand{\ing}[1]{\includegraphics[width=(\textwidth-6mm)/2,height=\textheight-6mm,page=#1]{empty}}
\newcommand{\pdf}[4]{
\hspace*{-11pt}%
\ing{\numexpr#1\relax}%
\ing{\numexpr#2\relax}%
\clearpage%
\hspace*{6pt}%
\ing{\numexpr#3\relax}%
\ing{\numexpr#4\relax}%
\clearpage%
}
\pagestyle{plain}
\newcounter{i}
\newcommand\maxpage{???}
\newcommand\emptypage{3}
\makeatletter
\newrobustcmd{\LastPageInFile}{%
\immediate\write\@auxout{\string
\newlabel{LastPageInDocument}{{}{\arabic{DocumentTotalPageCounter}}}}%
}% End of LastProblem
\newrobustcmd{\LastPartInFile}{%
\immediate\write\@auxout{\string
\newlabel{LastPartInDocument}{{}{\arabic{DocumentTotalPartCounter}}}}%
}% End of LastProblem
\makeatother
\AtEndDocument{%
\LastPartInFile%
\LastPageInFile%
}
\newcounter{DocumentTotalPartCounter}% No resetting
\preto{\part}{\stepcounter{DocumentTotalPartCounter}}%
\newcounter{DocumentTotalPageCounter}
\AddEverypageHook{\stepcounter{DocumentTotalPageCounter}}
\newrobustcmd{\GetLastPageNumber}[1][]{\pageref{#1LastPageInDocument}}%
\newrobustcmd{\GetLastPartNumber}[1][]{\pageref{#1LastPartInDocument}}%
\externaldocument[A-]{a}
\externaldocument[B-]{b}
\begin{document}
\textbf{\LARGE There are \GetLastPageNumber~pages in this document and it contains \GetLastPartNumber~parts}
\textbf{\LARGE There are \GetLastPageNumber[A-] pages in the other document and it contains \GetLastPartNumber[A-] parts}
\textbf{\LARGE There are \GetLastPageNumber[B-] pages in the other document and it contains \GetLastPartNumber[B-] parts}
\part{First}
\part{Second}
%%% Cover
%\pdf{1}{3}{3}{3}
%%% Inhalt
%\forloop{i}{1}{\value{i} < 34}{%
%\pdf{\thei*4+2}{\thei*4-1}{\thei*4}{\thei*4+1}
%}
%%% letzte 3 Seiten
%\pdf{3}{137}{138}{139}
\null\clearpage
% Maliciously setting page counter to zero!
\setcounter{page}{0}
% Reset part counter
\setcounter{part}{0}
\part{Starting over again}
\part{Starting over again, again;-)}
\end{document}
文件特克斯和豐特克斯readinpartnumber.tex
基本上与我删除了不必要代码的部分完全相同。
特克斯
\documentclass[twoside,landscape]{scrreprt}
\usepackage{etoolbox}
\usepackage{everypage}
\usepackage{forloop}
\usepackage{pdfpages}
\usepackage{background}
\usetikzlibrary{calc}
\usepackage[a4paper]{geometry}
\geometry{top=6mm, left=6mm, right=0mm, bottom=0mm}
\makeatletter
\newrobustcmd{\LastPageInFile}{%
\immediate\write\@auxout{\string
\newlabel{LastPageInDocument}{{}{\arabic{DocumentTotalPageCounter}}}}%
}% End of LastProblem
\newrobustcmd{\LastPartInFile}{%
\immediate\write\@auxout{\string
\newlabel{LastPartInDocument}{{}{\arabic{DocumentTotalPartCounter}}}}%
}% End of LastProblem
\makeatother
\AtEndDocument{%
\LastPartInFile%
\LastPageInFile%
}
\newcounter{DocumentTotalPartCounter}% No resetting
\preto{\part}{\stepcounter{DocumentTotalPartCounter}}%
\newcounter{DocumentTotalPageCounter}
\AddEverypageHook{\stepcounter{DocumentTotalPageCounter}}
\newrobustcmd{\GetLastPageNumber}{\pageref{LastPageInDocument}}%
\newrobustcmd{\GetLastPartNumber}{\pageref{LastPartInDocument}}%
\begin{document}
\textbf{\LARGE There are \GetLastPageNumber~pages in this document and it contains \GetLastPartNumber~parts}
\part{First}
\part{Second}
\null\clearpage
\label{somelabel}
% Maliciously setting page counter to zero!
\setcounter{page}{0}
% Reset part counter
\setcounter{part}{0}
\part{Starting over again}
\end{document}
豐特克斯
\documentclass[twoside,landscape]{scrreprt}
\usepackage{etoolbox}
\usepackage{everypage}
\usepackage{forloop}
\usepackage{pdfpages}
\usepackage{background}
\usetikzlibrary{calc}
\usepackage[a4paper]{geometry}
\geometry{top=6mm, left=6mm, right=0mm, bottom=0mm}
\makeatletter
\newrobustcmd{\LastPageInFile}{%
\immediate\write\@auxout{\string
\newlabel{LastPageInDocument}{{}{\arabic{DocumentTotalPageCounter}}}}%
}% End of LastProblem
\newrobustcmd{\LastPartInFile}{%
\immediate\write\@auxout{\string
\newlabel{LastPartInDocument}{{}{\arabic{DocumentTotalPartCounter}}}}%
}% End of LastProblem
\makeatother
\AtEndDocument{%
\LastPartInFile%
\LastPageInFile%
}
\newcounter{DocumentTotalPartCounter}% No resetting
\preto{\part}{\stepcounter{DocumentTotalPartCounter}}%
\newcounter{DocumentTotalPageCounter}
\AddEverypageHook{\stepcounter{DocumentTotalPageCounter}}
\newrobustcmd{\GetLastPageNumber}{\pageref{LastPageInDocument}}%
\newrobustcmd{\GetLastPartNumber}{\pageref{LastPartInDocument}}%
\begin{document}
\textbf{\LARGE There are \GetLastPageNumber~pages in this document and it contains \GetLastPartNumber~parts}
\part{First}
\part{Second}
\null\clearpage
\part{Appendix}
\part{Index}
\part{Bibliography}
\end{document}
笔记
快照中的这些行源自\backgroundsetup
代码
重要的
书面标签不符合 hyperref。
命令\GetLast...
和连接的代码应该存储在.sty
文件中!
目前,这只是一个 hack 行为,仅此而已。
答案2
我刚刚自己找到了一个答案(没有看到另一个):
A.tex
:
.
.
.
\newcommand\addprint{%
\newwrite\tmp
\immediate\openout\tmp=_drucken.txt
{\immediate\write\tmp{\thepage}}%
\immediate\closeout\tmp
}
\begin{document}
.
.
.
\addprint
\end{document}
_drucken.txt
:
139
B.tex
:
\documentclass[twoside,landscape]{scrreprt}
\usepackage{forloop}
\usepackage{pdfpages}
\usepackage{background}
\usepackage{xstring}
\usetikzlibrary{calc}
\usepackage[a4paper]{geometry}
\geometry{top=6mm, left=6mm, right=0mm, bottom=0mm}
\usepackage{xparse}
\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\ceil}{m}
{
\fp_eval:n { ceil ( #1 ) }
}
\ExplSyntaxOff
\backgroundsetup{
scale=1,
opacity=1,
angle=0,
color=black,
contents={%
\begin{tikzpicture}[remember picture,overlay]
\draw[thick] ($(current page.north west)+(0mm,-9mm)$) -- ++(\paperwidth,0mm); %o
\draw[thick] ($(current page.south west)+(0mm,9mm)$) -- ++(\paperwidth,0mm); %u
\draw[thick] ($(current page.north east)+(-9mm,0mm)$) -- ++(0,-\paperheight); %r
\draw[thick] ($(current page.north west)+(9mm,0mm)$) -- ++(0,-\paperheight); %l
\draw[thick] ($(current page.north)+(0mm,0mm)$) -- ++(0,-\paperheight); %m
\end{tikzpicture}%
}}
\pagestyle{plain}
\newcounter{i}
\def\empty{3}
\newcommand\getprint{%
\newread\tmp
\openin\tmp=_drucken.txt
\read\tmp to \maxpage
}
\newcommand{\chk}[1]{
\ifnum#1 > \numexpr\maxpage\relax
\empty
\else
#1
\fi}
\newcommand{\ing}[1]{\includegraphics[width=(\textwidth-6mm)/2,height=\textheight-6mm,page=\chk{#1}]{A.pdf}}
\newcommand{\pdf}[4]{
\hspace*{-11pt}%
\ing{\numexpr#1\relax}%
\ing{\numexpr#2\relax}%
\clearpage%
\hspace*{6pt}%
\ing{\numexpr#3\relax}%
\ing{\numexpr#4\relax}%
\clearpage%
}
\begin{document}
\getprint
%%% Cover
\pdf{1}{\empty}{\empty}{\empty}
%%% Inhalt
\forloop{i}{1}{\value{i} < \numexpr \ceil{\maxpage/4}+1 \relax}{%
\pdf{\thei*4+2}{\thei*4-1}{\thei*4}{\thei*4+1}
}
\null\clearpage
\end{document}