如何使用变量字符串重新定义花式标题和目录

如何使用变量字符串重新定义花式标题和目录

要求

(A)创建相同的文本,出现在(i)目录和(ii)右标题中;

(B) 减少代码中的冗余,以便我可以将相同的字符串传递给右标题和不可见部分;

(C)不制作任何额外页面

问题

以下代码满足要求 (A);但不满足 (B) 和 (C)。

如果我删除“\clearpage”,那么附录中的所有页面都会有相同的右标题 - 即最后一个不可见部分的右标题。

另外,请注意冗余,因为我必须传递两次字符串:一次用于标题,一次用于目录。

要求

请包含完全的解决方案的最小工作示例。

最小工作示例

\documentclass[12pt]{book}             

\usepackage{fancyhdr}

\newcommand\invisiblesection[1]{%
  \refstepcounter{section}%
  \addcontentsline{toc}{section}{\protect\numberline{\thesection}#1}%
  \sectionmark{#1}\phantom{}}
\makeatother



\begin{document}                 

\tableofcontents

\appendix
\addcontentsline{toc}{chapter}{Exhibits}
\def\thesection{Exhibit~\Roman{section}}
\pagestyle{fancy}

\newcommand{\exName}[3]{#1~#2~#3}

\invisiblesection{\exName{Student Portfolio}{\#1:}{Best}\label{ex:10}}
\rhead{\textbf{\thesection.~\exName{Student Portfolio}{\#1:}{Best}}}
\begin{figure}
\includegraphics{image1}
\end{figure}
\clearpage %% IF I REMOVE THIS, HEADER IS IDENTICAL THROUGHOUT

\invisiblesection{\exName{Student Portfolio}{\#2:}{Most-Improved}\label{ex:20}}
\rhead{\textbf{\thesection.~\exName{Student Portfolio}{\#2:}{Most-Improved}}}
\begin{figure}
\includegraphics{image2}
\end{figure}

\end{document}

答案1

我不完全确定您想要的效果,但以下方法解决了我在运行 MWE 时发现的一些目录和页眉问题,并且还处理了一些冗余。页眉的问题取决于分页的位置——您将获得页面上声明的最后一个页眉,因此我添加了一些拉丁文来说明。结果是几页,所以我省略了图像。

    \documentclass[12pt]{book}             

    \usepackage{fancyhdr}
    \usepackage{xparse}
    \usepackage{lipsum}

    \newcounter{exhibits}

    \DeclareDocumentCommand{\invisiblesection}{m}{%
      \refstepcounter{section}%
      \refstepcounter{exhibits}%
      \addcontentsline{toc}{section}{\protect\numberline{}#1}%
      \rhead{\textbf{#1}}% N.B.: this will take the last value declared when the page is cut, and if your images are floats, they may fall on a later page.
      \phantom{}
    }

    \newcommand{\sectionlabel}{Exhibit~\makebox[2em][l]{\Roman{section}.}} % to avoid some of the toc issues; could make "Exhibit" an argument instead.
    \newcommand{\exName}[3]{#1~#2~#3} % note, put definitions in preamble

    \lhead{} % clear the left header

    \begin{document}                 

    \pagestyle{plain} % for your toc page
    \tableofcontents


    \appendix
    \addcontentsline{toc}{chapter}{\protect\numberline{}Exhibits}
    \newpage % put the toc on a separate page

    \pagestyle{fancy} % for the exhibits.

    {% to keep section and floating figure together
    \invisiblesection{\sectionlabel\exName{Student Portfolio}{\#\arabic{exhibits}:}{Best}\label{ex:10}}
    \begin{figure}[h]
    \textbf{Best one.} \lipsum[1]
    \centering\rule{1em}{20em} % stand-in for the figure
    %\includegraphics{image1}
    \end{figure}
    }

    {
    \invisiblesection{\sectionlabel\exName{Student Portfolio}  {\#\arabic{exhibits}:}{Most Improved}\label{ex:20}}
    \textbf{Most improved one.} \lipsum[1]
    \begin{figure}[h]
    \centering\rule{1em}{35em} % to stand for the figure
    %\includegraphics{image2}
    \end{figure}
    }

    {
    \textbf{Most thoughtful one.} This is a great student. I wish I had more like this one. Some one who is actually interested in what I am teaching, as opposed to getting a nice grade and then going on to Wall Street.
    % just leaving the figure environment out. 

    \centering\rule{1em}{25em} % to stand for the figure
    %\includegraphics{image2}
    \invisiblesection{\sectionlabel\exName{Student Portfolio}{\#\arabic{exhibits}:}{Most Thoughtful}\label{ex:20}}
    }

    \end{document}

答案2

以下是我最终的选择:

此代码满足我的所有要求。尽管我不知道如何避免在 includegraphics 命令后手动添加 \clearpage 命令。

最小工作示例

\documentclass[12pt]{book}
\usepackage{lipsum} % delete this as necessary
\usepackage{fancyhdr}
\usepackage{pdfpages}
\usepackage{lastpage}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%% APPENDIX STYLE %%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcounter{exhibits}
\DeclareRobustCommand{\invisiblesection}[2]{%
\refstepcounter{exhibits}
\refstepcounter{section}%
\sectionmark{#1}
\addcontentsline{toc}{section}{\protect\numberline{}#1#2}
\rhead{\textbf{#2}}
\lhead{\textbf{#1}}
%\rfoot{\textbf{2}}
%\lfoot{\textbf{2}}
\pagestyle{fancy} % for the exhibits.
\fancyfoot[LE,RO]{\bfseries \thepage~of~\pageref{LastPage}}
\fancyfoot[CO,CE]{}
\fancyfoot[LO,RE]{\bfseries My Document}
}
\newcommand{\sectionlabel}{Exhibit~\makebox[3.5em][l]{\Roman{section}.}}
\newcommand{\exName}[3]{#1~#2~#3} 
\def\thesection{Exhibit~\Roman{section}}
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%% MAIN BODY STYLE %%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}
\fancyhf{}
\fancyhead[LE,RO]{\bfseries\thepage}
\fancyhead[LO]{\bfseries\rightmark}
\fancyhead[RE]{\bfseries\leftmark}
\fancypagestyle{plain}{%
\fancyhead{} % get rid of headers
\renewcommand{\headrulewidth}{0pt} % and the line
}

\begin{document} 

\frontmatter

\tableofcontents


\mainmatter 
\chapter{First\label{ch:one}}
In Chapter~\ref{ch:one} we discuss info in \ref{ex:2-10} on Page~\pageref{ex:2-10}. 
Also we discuss the info in  \ref{ex:2-60} on Page~\pageref{ex:2-60} and  \ref{ex:2-70} on Page~\pageref{ex:2-70}. 


\lipsum[90]\lipsum[90]\lipsum[90]


\appendix
\newpage
\addcontentsline{toc}{chapter}{\protect\numberline{}Exhibits} % Puts `Exhibits in table of contents
%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%
{\invisiblesection{\sectionlabel}{\exName{Head}{\#\arabic{exhibits}:}{Subhead}} 
\label{ex:2-10}
\includepdf[scale=0.8,pages={1},pagecommand={}, offset=0 -18]{*.pdf}}
%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%
\setcounter{exhibits}{0}
{\invisiblesection{\sectionlabel}{\exName{Sample}{\#\arabic{exhibits}:}{Best}}
\label{ex:2-60}
\begin{figure}[!ht]
\begin{center}
%\includegraphics{*.*}
\end{center}
\end{figure}
}
\clearpage
%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%
{\invisiblesection{\sectionlabel}{\exName{Sample}{\#\arabic{exhibits}:}{Poorest}}
\label{ex:2-70}
\begin{figure}[!ht]
\begin{center}
%\includegraphics{*.*}
\end{center}
\end{figure}}
\clearpage

\end{document}

相关内容