![使用 pdfpages 包含与长度相关的页面范围](https://linux22.com/image/453576/%E4%BD%BF%E7%94%A8%20pdfpages%20%E5%8C%85%E5%90%AB%E4%B8%8E%E9%95%BF%E5%BA%A6%E7%9B%B8%E5%85%B3%E7%9A%84%E9%A1%B5%E9%9D%A2%E8%8C%83%E5%9B%B4.png)
使用补丁prelast
定义pdf页面抛出错误。[这可能与 pdfpages 中的更新有关吗?]
我正在使用使用 pdfpages 包含与长度相关的页面范围作者:@Werner,编辑:@DavidCarlisle。
使用该命令
\includepdf[pages=1-3]{dummy-l.pdf}
一切正常。但是当我尝试时
\includepdf[pages=1-prelast]{dummy-l.pdf}
,我收到错误:
包 pdfpages 错误:找不到文件“1-prelast”。
目标是读取不同长度的 PDF 并为文本的最后一页着色。当我删除\usepackage{xcolor}
所有\pagecolor{}
命令时,我仍然收到相同的错误。但是,由于颜色部分对我的项目至关重要,因此我将其留在代码中以防万一。
请注意,一旦此错误得到修复,我就会有来自该项目的其他问题,但我不想在这里添加它们。
\documentclass{amsart}
\usepackage[usenames,dvipsnames,svgnames]{xcolor}
\pagecolor{white}
\usepackage{pdfpages}% http://ctan.org/pkg/pdfpages
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DEFINE “prelast” to contain the pagenumber of the next-to-last page
% in pdfpages — from answer by Werner and David Carlisle found at
% https://tex.stackexchange.com/questions/62911/including-a-length-dependent-page-range-using-pdfpages
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatletter
\def\AM@checklast#1-\END{%
\edef\AM@tempii{\AM@trim@space{#1}}%
\expandafter\ifx\expandafter\\\AM@tempii\\
\@tempcntb=\AM@pagecount\relax
\ifAM@DVIoutput
\ifnum\AM@pagecount=1
\@tempcntb\@tempcnta
\fi
\fi
\advance\@tempcntb\@ne
\whiledo{\@tempcnta<\@tempcntb}{%
\AM@append{\the\@tempcnta}%
\advance\@tempcnta\@ne
}%
\else
\def\AM@cmp{last}%
\ifx\AM@cmp\AM@tempii
\let\AM@tempii\AM@pagecount
\else\def\AM@cmp{prelast}% <------------------------- ADDED
\ifx\AM@cmp\AM@tempii% |
\@tempcntb=\numexpr\number\AM@pagecount-1\relax% |
\edef\AM@tempii{\the\@tempcntb}% |
\fi% <---------------------------------------------------
\fi
\AM@checkinteger{\AM@tempii}%
\ifAM@integer
\@tempcntb=\AM@tempii\relax
\AM@checkpagenumber{\the\@tempcntb}%
\ifnum\@tempcnta<\@tempcntb \def\AM@rel{<}\def\AM@inc{\@ne}%
\else \def\AM@rel{>}\def\AM@inc{\m@ne}\fi
\advance\@tempcntb\AM@inc
\whiledo{\@tempcnta\AM@rel\@tempcntb}{%
\AM@append{\the\@tempcnta}%
\advance\@tempcnta\AM@inc
}%
\else
\AM@setnewdocname{\AM@temp}%
\fi
\fi
}
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\pagecolor{cyan!50!white}
Some Text
\clearpage % before changing color must be on next page
\pagecolor{white}
\includepdf[pages=1-prelast]{dummy-l.pdf} % <----- error
% can't find file `1-prelast'.
%\includepdf[pages=1-3]{dummy-l.pdf} <----- works
\pagecolor{pink}
\includepdf[pages=last]{dummy-l.pdf} % <----- works
%\includepdf[pages=4]{dummy-l.pdf} <----- works
\pagecolor{white}
Some more text.
\end{document}
答案1
我不会像这样修补 pdfpages(当然也不会使用 10 年前的答案中的代码)。我会像在其他答案中一样检索页码,或者在更现代的版本中检索页码。
附注:usenames 选项已过时,并将在未来的版本中删除。
\documentclass{amsart}
\usepackage{l3graphics}
\usepackage[dvipsnames,svgnames]{xcolor}
\pagecolor{white}
\usepackage{pdfpages}% http://ctan.org/pkg/pdfpages
\ExplSyntaxOn
\newcommand\prelast{}
\newcommand\getprelast[1]
{
\graphics_get_pagecount:nN{#1}\l_tmpa_tl
\tl_set:Nx\prelast{\int_eval:n{\l_tmpa_tl-1}}
}
\ExplSyntaxOff
\begin{document}
\pagecolor{cyan!50!white}
Some Text
\clearpage % before changing color must be on next page
\pagecolor{white}
\getprelast{example-image-a4-numbered.pdf}
\includepdf[pages=1-\prelast]{example-image-a4-numbered}
\pagecolor{pink}
\includepdf[pages=last]{example-image-a4-numbered}
\pagecolor{white}
Some more text.
\end{document}