quickcode.tex

quickcode.tex

我想使用以下代码将 PDF 文档包含在我的 LaTeX 文档中:

\documentclass[12pt,a4paper,fleqn,parskip=half]{scrartcl}

\usepackage{lscape}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage[scaled]{helvet}
\usepackage{caption}
\usepackage[breaklinks=true,colorlinks=true,linkcolor=black,citecolor=black,urlcolor=black]{hyperref}
\usepackage{xargs}                                          


%---------------------------------------------------------------------
%   % Macro to find the number of pages of included PDF document
%---------------------------------------------------------------------
\makeatletter
\newcounter{imagepage}
\newcommand*{\foreachpage}[2]{
  \begingroup
   \sbox0{\includegraphics{#1}}
    \xdef\foreachpage@num{\the\pdflastximagepages}
  \endgroup
  \setcounter{imagepage}{0} 
  \@whilenum\value{imagepage}<\foreachpage@num\do{
    \stepcounter{imagepage}
    #2\relax
  }
}
\makeatother


%---------------------------------------------------------------------
%   % Macro to include PDF document with page numbers in caption and label
%---------------------------------------------------------------------
\makeatletter
\newcommandx{\picTEST}[5][1=0.9, 2=0]
{
    \foreachpage{#4}
 {
  \begin{figure}[h!]
   \centering
   \makebox[\textwidth]{\includegraphics[width=#1\hsize,page=\value{imagepage},angle=#2]{#4}}
   \label{#3\theimagepage}
     \caption{#5 - Page \arabic{imagepage} of {\the\pdflastximagepages}}
  \end{figure}
  \clearpage
 }  
}
\makeatother


\begin{document}

\section{Some Text}
Here is reference to page \ref{Document1_Label_Page3} of the included PDF document.

\newpage
\section{Some insert of PDF document}
\picTEST{Document1_Label_Page}{Document1.pdf}{Document1}

\end{document}

问题是文本中的标签没有扩展为数字“3”,而是得到了“??”。但是,当我单击文本中的“??”时,对所包含 PDF 文档的页码 3 的交叉引用可以正常工作 - 它会跳转到此页面(或者更确切地说是跳转到此图)。我猜问题出在我\picTEST使用的第二个宏中\label{#3\theimagepage},也许出在我引用的文本中\ref{Document1_Label_Page3}。我非常恳请您提供支持。请使用名为 的一些示例多页 PDF 文档Document1.pdf

答案1

\label宏中的命令必须\picTEST位于 之后\caption,因为它需要\refstepcounter调用,而调用是在 内完成的\caption。否则标签会指向其他内容或无处可寻。

此外,该文件必须具有相应的应引用的页数。

\documentclass[12pt,a4paper,fleqn,parskip=half]{scrartcl}

\usepackage{lscape}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[demo]{graphicx}
\usepackage[scaled]{helvet}
\usepackage{caption}
\usepackage[breaklinks=true,colorlinks=true,linkcolor=black,citecolor=black,urlcolor=black]{hyperref}
\usepackage{xargs}                                          


%---------------------------------------------------------------------
%   % Macro to find the number of pages of included PDF document
%---------------------------------------------------------------------
\makeatletter
\newcounter{imagepage}
\newcommand*{\foreachpage}[2]{
  \begingroup
   \sbox0{\includegraphics{#1}}
    \xdef\foreachpage@num{\the\pdflastximagepages}
    \endgroup
  \setcounter{imagepage}{0} 
  \@whilenum\value{imagepage}<\foreachpage@num\do{%
    \stepcounter{imagepage}
    #2\relax
  }
}
\makeatother


%---------------------------------------------------------------------
%   % Macro to include PDF document with page numbers in caption and label
%---------------------------------------------------------------------
\makeatletter
\newcommandx{\picTEST}[5][1=0.9, 2=0]
{
  \foreachpage{#4}
 {
   \begin{figure}[h!]
     \centering
     \makebox[\textwidth]{\includegraphics[width=#1\hsize,page=\value{imagepage},angle=#2]{#4}}
     \caption{#5 - Page \arabic{imagepage} of {\the\pdflastximagepages}}
     \label{#3\theimagepage}
   \end{figure}
   \clearpage
 }  
}
\makeatother


\begin{document}

\section{Some Text}
Here is reference to page \ref{Document1_Label_Page3} of the included PDF document.

\newpage
\section{Some insert of PDF document}
\picTEST{Document1_Label_Page}{quickcode.pdf}{Document1}

\end{document}

quickcode.tex

\documentclass{book}


\usepackage{forloop}

\newcounter{loopcounter}
\usepackage{blindtext}

\begin{document}
\forloop{loopcounter}{1}{\value{loopcounter} < 11}{%
\blindtext[\number\value{loopcounter}]
\newpage
}%



\end{document}

在此处输入图片描述

相关内容