如何访问页脚中使用的每页变量/命令

如何访问页脚中使用的每页变量/命令

我想要实现的是在标题中包含每个章节的变量/命令(或页脚,会是同样的问题)。

我认为这个问题的真正关键是:如何在页眉/页脚中包含一个在运行时而不是在定义时评估的命令?

在我的特定用例中,每个章节都是独立“审阅”的,我想在页脚中包含审阅日期。这些审阅日期在章节的某个地方定义(到目前为止,我尝试使用\let和)。\def

以下是最小的例子:

\documentclass[9pt]{memoir}

\usepackage{xcolor}
\definecolor{accentcolor}{HTML}{D33900}
\definecolor{fadedgreycolor}{HTML}{888888}
\definecolor{shadecolor}{HTML}{FED6C7}
\usepackage{longtable}
\usepackage{eso-pic}
\usepackage{ifthen}
\usepackage[colorlinks=true, urlcolor=accentcolor]{hyperref}

\urlstyle{same}

% Chapter styles.
\copypagestyle{chapter}{plain}
\setlength{\beforechapskip}{0pt}
\setlength{\afterchapskip}{10pt}
\renewcommand*{\chaptitlefont}{\normalfont\Huge\bfseries\color{accentcolor}}
\makeevenfoot{chapter}{\color{fadedgreycolor}\thepage{} \reviewedblurb}{}{}
\makeoddfoot{chapter}{}{}{\color{fadedgreycolor}\thepage{} \reviewedblurb}

% Header/footer styles.
\copypagestyle{blah}{headings}
\makeevenhead{blah}{\color{fadedgreycolor}\leftmark}{}{}
\makeoddhead{blah}{}{}{\color{fadedgreycolor}\leftmark}
\makeevenfoot{blah}{\color{fadedgreycolor}\thepage}{}{}
\makeoddfoot{blah}{}{}{\color{fadedgreycolor}\thepage}
\nouppercaseheads
\makerunningwidth{blah}{\textwidth}
\makeheadrule{blah}{\textwidth}{\normalrulethickness}
\makeheadfootruleprefix{blah}{\color{fadedgreycolor}}{\color{fadedgreycolor}}
\pagestyle{blah}

\newcommand{\reviewedblurb}{\ifthenelse{\isundefined{\lastreviewedon}}{}{Reviewed on: \lastreviewedon}}

\tightlists

\begin{document}

\tableofcontents

\def \lastreviewedon {August 07, 2015}

\chapter{This is the first Chapter}

\section{There are 8 key features in this topic:}

\begin{enumerate}
\item This is item 1
  \begin{enumerate}
    \item This is sub item 1.
  \end{enumerate}
\end{enumerate}

\def \lastreviewedon {July 07, 2015}

\chapter{This is the second Chapter}

\section{There are 5 key features in this topic:}

\begin{enumerate}
\item This is item 1
  \begin{enumerate}
    \item This is sub item 1.
  \end{enumerate}
\end{enumerate}

\end{document}

答案1

下面提供了一种显示“上次查看日期”的方法。它\label使用\refzref创建一个名为的“属性” lastrevieweddate,用于存储提供给的最后一个已知参数\lastreviewedon{<stuff>}

在此处输入图片描述

\documentclass{memoir}

\usepackage{xcolor,zref}
\definecolor{accentcolor}{HTML}{D33900}
\definecolor{fadedgreycolor}{HTML}{888888}
\definecolor{shadecolor}{HTML}{FED6C7}
\usepackage{lipsum}
\usepackage[colorlinks=true, urlcolor=accentcolor]{hyperref}

% Chapter styles.
\copypagestyle{chapter}{plain}
\setlength{\beforechapskip}{0pt}
\setlength{\afterchapskip}{10pt}
\renewcommand*{\chaptitlefont}{\normalfont\Huge\bfseries\color{accentcolor}}
\makeevenfoot{chapter}{\color{fadedgreycolor}\thepage{} \reviewedblurb}{}{}
\makeoddfoot{chapter}{}{}{\color{fadedgreycolor}\thepage{} \reviewedblurb}

% Header/footer styles.
\copypagestyle{blah}{headings}
\makeevenhead{blah}{\color{fadedgreycolor}\leftmark}{}{}
\makeoddhead{blah}{}{}{\color{fadedgreycolor}\leftmark}
\makeevenfoot{blah}{\color{fadedgreycolor}\thepage}{}{}
\makeoddfoot{blah}{}{}{\color{fadedgreycolor}\thepage}
\nouppercaseheads
\makerunningwidth{blah}{\textwidth}
\makeheadrule{blah}{\textwidth}{\normalrulethickness}
\makeheadfootruleprefix{blah}{\color{fadedgreycolor}}{\color{fadedgreycolor}}
\pagestyle{blah}

\makeatletter
\zref@newprop{lastrevieweddate}[??]{\@empty}
\newcommand{\reviewedblurb}{%
  \zref@ifrefundefined{last-review-date-\thechapter}{}
    {\zref@def@extractdefault{\@lastrevieweddate}{last-review-date-\thechapter}{lastrevieweddate}{\@empty}%
     \ifx\@lastrevieweddate\@empty\else
       Reviewed on: \zref@extract{last-review-date-\thechapter}{lastrevieweddate}%
     \fi}}
\newcommand{\lastreviewedon}[1]{\zref@setcurrent{lastrevieweddate}{#1}}
\let\oldchapter\chapter
\renewcommand{\chapter}{%
  \ifnum\value{chapter}<1\else% Nothing to do before the first chapter
    \zref@labelbyprops{last-review-date-\thechapter}{lastrevieweddate}%
    \zref@setcurrent{lastrevieweddate}{}% Clear any review date references for the next chapter
  \fi
  \oldchapter% Resume old \chapter sequence
}
\AtEndDocument{\zref@labelbyprops{last-review-date-\thechapter}{lastrevieweddate}}%
\makeatother

\begin{document}

\tableofcontents

\chapter{First chapter}
\lastreviewedon{August 07, 2015}% Last reviewed date noted at start of chapter
\lipsum[1-20]

\chapter{Second chapter}
\lipsum[1-20]% No reviewed date specified

\chapter{Third chapter}
\lastreviewedon{January 01, 2015}% Multiple instances of last reviewed date
\lastreviewedon{January 02, 2015}% This will be used
\lipsum[1-20]

\chapter{Fourth chapter}
\lipsum[1-20]
\lastreviewedon{July 07, 2015}% Last reviewed date noted at end of chapter

\end{document}

由于该方法基于使用(交叉)引用,因此您可以将它放置在范围\lastreviewedon{<stuff>}内的任何位置\chapter。请注意,这还需要在第一次尝试时至少进行两次编译(典型的交叉引用用法)。作为奖励,还允许多个实例,最后一个实例用于引用。

相关内容