获取迷你页面的真实页码

获取迷你页面的真实页码

我无法获取某个部分中的 的“真实”页码minipage。有时它会差一页。我试图将框的页码和位置(以毫米为单位)(它在 中\fbox)传递给“附加到字符串”函数,以便我可以稍后写出位置。

如果我使用\def\pageontest{\immediate\pageref{\upn/\grade/\thesection}},那么\pageontest看起来是正确的,因为当我在下一行打印它时它是正确的。但是当我尝试传递\pageontest给时,\appendpositionstring我收到编译错误。我以前只是传递\thepage\appendpositionstring但有时这是不正确的(例如,当fbox/minipage/section最初可能放在上一页的末尾但在编译时最终出现在下一页时)。

\newcommand\appendpositionstring[1]{

    \xdef\positionstring{\positionstring/#1}
}

\newlength{\boxwidth}

\newcommand{\question}[3]{%
  \section{\grade/\thesection}
  #2\\
          \zsavepos{\upn/\thesection-ll}{
            \fbox{%

            \setlength{\boxwidth}{0.92\columnwidth}
            \begin{minipage}[t][#1][t]{\boxwidth}\label{\upn/\grade/\thesection}
            #3 
            \def\pageontest{\immediate\pageref{\upn/\grade/\thesection}}
            Page: \pageontest
            \appendpositionstring{\pageontest,\thesection,\getPOS{\upn/\thesection-ll}{\boxwidth}{#1}}
          \end{minipage}
          }
    }

            %PosString: \positionstring
}

类文件:

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{gradetrakexam}[2015/01/15]
\LoadClass{article}
\RequirePackage[margin=1.2cm,bmargin=3.3cm,footskip=0.3cm,headsep=0.1cm]{geometry}
\RequirePackage[T1]{fontenc}
\RequirePackage{lmodern}
\RequirePackage{amsmath}
\RequirePackage{enumerate}
\RequirePackage{fancyhdr}
\RequirePackage{xcolor}
\RequirePackage{qrcode}
\RequirePackage{zref-user}
\RequirePackage{zref-abspos}
% ifthen package
\RequirePackage{ifthen}
% For writing out the page number of boxes
\RequirePackage{refcount}
%\RequirePackage{fontspec}
%\setmainfont[Ligatures=TeX]{Cambria}
%\defaultfontfeatures[Ligatures=TeX]{Cambria} % To support LaTeX quoting style
\RequirePackage[compact]{titlesec}
\titleformat{\section}[runin]         % Customise the \section command 
  {\normalsize\bf} %
  {Skill }{0em}                      % Can be used to give a prefix to all sections, like 'Section ...'
  {}                           % Can be used to insert code before the heading
\date{}

% enable stamps
\newif\if@STAMP\@STAMPtrue
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}
\ProcessOptions
\usepackage{pgfmath,pgfkeys}
\RequirePackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usetikzlibrary{decorations.pathmorphing}

\def\upn{theupn}
\def\@studentstring{thestudentstring}
\def\grade{thegrade}
\def\testnum{thetestnum}
\def\positionstring{}
\fancypagestyle{stamp}{
  \setlength{\headheight}{35pt}
  \fancyhf{}% Clear header/footer
  \renewcommand{\headrulewidth}{0pt} % remove lines as well
  \renewcommand{\footrulewidth}{0pt}
  \lhead{\LARGE{\textbf{\colorbox{yellow}{\@studentstring }}}}
  \rhead{{\small \thepage}}
  \cfoot{\gradetrakpagemark}
}
\pagestyle{stamp}

\newcommand\printheading{
\if@twocolumn%
    \twocolumn[
    \begin{center}
    {\LARGE \textbf{Grade \grade, test \testnum}}
    \end{center}
    ]
\else% \@twocolumnfalse
    \begin{center}
    {\LARGE \textbf{Grade \grade, test \testnum}}
    \end{center}
\fi%
}

\def\dimtomm #1{\the\numexpr \dimexpr #1\relax*635/118407168\relax }

\newcommand\getPOS[3]{%
    \dimtomm{\zposx{#1}sp},\dimtomm{\zposy{#1}sp},\dimtomm{#2},\dimtomm{#3}
}

\newcommand\appendpositionstring[1]{%
    \xdef\positionstring{\positionstring/#1}%
}
\newlength{\boxwidth}

\newcommand{\question}[3]{%
  \section{\grade/\thesection}
  #2\\
          \zsavepos{\upn/\thesection-ll}{
            \fbox{%

            \setlength{\boxwidth}{0.92\columnwidth}
            \begin{minipage}[t][#1][t]{\boxwidth}\label{\upn/\grade/\thesection}
            #3 
            \def\pageontest{\immediate\pageref{\upn/\grade/\thesection}}
            Page: \pageontest
            \appendpositionstring{\pageontest,\thesection,\getPOS{\upn/\thesection-ll}{\boxwidth}{#1}}
          \end{minipage}
          }
    }

            PosString: \positionstring
}

% QR Code implementation
\RequirePackage{qrcode}

\def\@gradetrak@STAMP{%
  \begingroup
    \begin{scope}[line width=\elementlinewidth, shift={(current page.south west)}]%
          \begin{scope}[shift={($(\edgeleftmargin, \edgebottommargin)$)}]%
            \node(barcode)[anchor=south west,outer sep=0,inner sep=0]{\qrcode[version=3,level=H,padding,height=20mm]{\upn}};%
          \end{scope}
        % We unconditionally print this barcode, it is required for the recognition
        % process.
        \begin{scope}[shift={($(\paperwidth, 0) + (-\edgerightmargin, \edgebottommargin)$)}]%
%%%
% bottom right bar code contains
% list of question numbers and measurements in mm relative to 
% bottom left of page of each question box
% eg 2,1,34.343434343,15.34343434,75.324234,30.34343
%    Page,Q,x,y,w,h
% multiple Qs concatened with a "/" character as separator.
%%%
          \edef\barcodechars{PAGE:\thepage+POSSTRING:\positionstring}%
          \node(barcode)[anchor=south east,outer sep=0,inner sep=0]{\qrcode[version=4,level=H,padding,height=20mm]{\barcodechars}};%
        \end{scope}
% also put grade and test num in central bar code
          \begin{scope}[shift={($(\paperwidth/2-\edgerightmargin/2+\edgeleftmargin/2, \edgebottommargin)$)}]%
            \node(barcode)[anchor=south,outer sep=0,inner sep=0]{\qrcode[version=3,level=H,padding,height=20mm]{\grade/\testnum}};%
          \end{scope}
    \end{scope}%
  \endgroup
}


\def\gradetrakpagemark{%
  \begin{tikzpicture}[remember picture,overlay]%
  %---------------------------------------------------------------------------
    % stamp
    %---------------------------------------------------------------------------
    \if@STAMP
        % Always stamp
        \@gradetrak@STAMP
    \fi
  \end{tikzpicture}%
}

\newlength{\edgeleftmargin}
\setlength{\edgeleftmargin}{12mm}
\newlength{\edgerightmargin}
\setlength{\edgerightmargin}{12mm}
\newlength{\edgetopmargin}
\setlength{\edgetopmargin}{12mm}
\newlength{\edgebottommargin}
\setlength{\edgebottommargin}{5mm}
\newlength{\elementlinewidth}
\setlength{\elementlinewidth}{1.0bp}

%%%
% declare exampaper environment
%%%

\newenvironment{exampaper}[4]{% 
% 'header' to environemnt
\setcounter{section}{0}
\setcounter{page}{1}
\pagestyle{empty}
\def\grade{#3}
\def\testnum{#4}
\def\@studentstring{#2}
\def\upn{#1}
\xdef\positionstring{}
\pagestyle{stamp}
\printheading
}
{
% 'footer' to environment
\clearpage
}

该文件:

\documentclass[a4paper,10pt,twocolumn,landscape]{gradetrakexam}
\begin{document}

\begin{exampaper}{UPN1231321}{SMITH Bill}{A}{2}
\question{8cm}{Question text above}{%
Data that would go in box
}

\question{4.6cm}{Could you answer a question in this box?}{}

\question{4cm}{Evaluate: $\displaystyle \int_{0}^{\pi}{4\sin{2\theta}d\theta}$}{}

\question{3cm}{Find $\displaystyle\frac{dy}{dx}$, given that $y=2e^{3x} + 5x^2$}{}


\question{15cm}{Solve the integrals:}{
    \begin{enumerate}[a).]
    \item $\displaystyle I=\int{3x^2 dx}$ \vskip 5cm
    \item $\displaystyle \int{5\tan{2x}dx}$\vskip 5cm
    \end{enumerate}
}  

\question{8cm}{Complete the table for $y=2x^2 + 3x -1$}{
\centering\begin{tabular}{ l c r }
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\end{tabular}
}
\end{exampaper}


\begin{exampaper}{UPN23423894}{JONES Tom}{C}{2}

\question{4cm}{Evaluate: $\displaystyle \int_{0}^{\pi}{4\sin{2\theta}d\theta}$}{}
\definecolor {processblue}{cmyk}{0.96,0,0,0}

\question{10cm}{Find $\displaystyle\frac{dy}{dx}$, given that $y=2e^{3x} + 5x^2$}{
\begin {tikzpicture}[-latex ,auto ,node distance =4 cm and 5cm ,on grid ,
semithick ,
state/.style ={ circle ,top color =white , bottom color = processblue!20 ,
draw,processblue , text=blue , minimum width =1 cm}]
\node[state] (C)
{$1$};
\node[state] (A) [above left=of C] {$0$};
\node[state] (B) [above right =of C] {$2$};
\path (A) edge [loop left] node[left] {$1/4$} (A);
\path (C) edge [bend left =25] node[below =0.15 cm] {$1/2$} (A);
\path (A) edge [bend right = -15] node[below =0.15 cm] {$1/2$} (C);
\path (A) edge [bend left =25] node[above] {$1/4$} (B);
\path (B) edge [bend left =15] node[below =0.15 cm] {$1/2$} (A);
\path (C) edge [bend left =15] node[below =0.15 cm] {$1/2$} (B);
\path (B) edge [bend right = -25] node[below =0.15 cm] {$1/2$} (C);
\end{tikzpicture}
}

\end{exampaper}

\end{document}

答案1

\newcommand\appendpositionstring我将有关、\newlength{\boxwidth}和的代码纳入\newcommand{\question}了您的 MWE,并进行了一些更改以使其可编译:\appendpositionstring\question已经定义,因此有必要使用\renewcommand而不是\newcommand。长度\boxwidth已经存在,因此不需要再次创建。 我从定义中删除了空行,并通过%在行尾添加百分号 ( ) 来“吞噬”空格。 为了从参考文献中提取页码,我使用了引用计数包及其命令\getpagerefnumber。这样,示例就可编译了。要获取正确的页码,\pagebreak在 之前必须有\question{15cm}{Solve the integrals:}{ ... }。请注意,在第三页(再次命名为“1”)上,\pageontest现在是“1”而不是“3”。如果需要“3”,请执行以下操作:删除带有 的行,在和之间\edef\pageontest{\getpagerefnumber{\upn/\grade/\thesection}}%添加带有 的行,替换\edef\pageontest{\theCurrentPage}%\label{\upn/\grade/\thesection}%#3 %\usepackage{refcount}为。\usepackage{页面}\pagenumbering{arabic}并进行几次编译器运行。(手动设置问题的必要性\pagebreak仍然存在。)

\documentclass[a4paper,10pt,twocolumn,landscape]{gradetrakexam}

\usepackage{refcount}

\renewcommand\appendpositionstring[1]{%
  \xdef\positionstring{\positionstring/#1}%
 }

\renewcommand{\question}[3]{%
  \section{\grade/\thesection}%
  #2\\ %
  \zsavepos{\upn/\thesection-ll}{%
    \fbox{%
      \setlength{\boxwidth}{0.92\columnwidth}%
      \begin{minipage}[t][#1][t]{\boxwidth}%
        \label{\upn/\grade/\thesection}%
        #3 %
        \edef\pageontest{\getpagerefnumber{\upn/\grade/\thesection}}%
        Page: \pageontest%
        \appendpositionstring{\pageontest,\thesection,\getPOS{\upn/\thesection-ll}{\boxwidth}{#1}}%
      \end{minipage}%
     }%
   }%
  % PosString: \positionstring
 }

\begin{document}

\begin{exampaper}{UPN1231321}{SMITH Bill}{A}{2}
\question{8cm}{Question text above}{%
Data that would go in box%
}

\question{4.6cm}{Could you answer a question in this box?}{}

\question{4cm}{Evaluate: $\displaystyle \int_{0}^{\pi}{4\sin{2\theta}d\theta}$}{}

\question{3cm}{Find $\displaystyle\frac{dy}{dx}$, given that $y=2e^{3x} + 5x^2$}{}

\pagebreak
\question{15cm}{Solve the integrals:}{
    \begin{enumerate}[a).]
    \item $\displaystyle I=\int{3x^2 dx}$ \vskip 5cm
    \item $\displaystyle \int{5\tan{2x}dx}$\vskip 5cm
    \end{enumerate}
}  


\question{8cm}{Complete the table for $y=2x^2 + 3x -1$}{
\centering\begin{tabular}{ l c r }
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\end{tabular}
}
\end{exampaper}


\begin{exampaper}{UPN23423894}{JONES Tom}{C}{2}

\question{4cm}{Evaluate: $\displaystyle \int_{0}^{\pi}{4\sin{2\theta}d\theta}$}{}
\definecolor {processblue}{cmyk}{0.96,0,0,0}

\question{10cm}{Find $\displaystyle\frac{dy}{dx}$, given that $y=2e^{3x} + 5x^2$}{
\begin {tikzpicture}[-latex ,auto ,node distance =4 cm and 5cm ,on grid ,
semithick ,
state/.style ={ circle ,top color =white , bottom color = processblue!20 ,
draw,processblue , text=blue , minimum width =1 cm}]
\node[state] (C)
{$1$};
\node[state] (A) [above left=of C] {$0$};
\node[state] (B) [above right =of C] {$2$};
\path (A) edge [loop left] node[left] {$1/4$} (A);
\path (C) edge [bend left =25] node[below =0.15 cm] {$1/2$} (A);
\path (A) edge [bend right = -15] node[below =0.15 cm] {$1/2$} (C);
\path (A) edge [bend left =25] node[above] {$1/4$} (B);
\path (B) edge [bend left =15] node[below =0.15 cm] {$1/2$} (A);
\path (C) edge [bend left =15] node[below =0.15 cm] {$1/2$} (B);
\path (B) edge [bend right = -25] node[below =0.15 cm] {$1/2$} (C);
\end{tikzpicture}
}

\end{exampaper}


\end{document}

相关内容