0

Requirements

(A) create identical text to appear in both (i) the table of contents and (ii) the right header;

(B) reduce the redundancy in my code so that I can pass the same string to both right header and invisible section;

(C) do not produce any extra pages

Problem

The code below meets requirements (A); but not (B) and (C).

If I remove `\clearpage', then all pages in the appendix have the same rightheader -- namely the right header of the last invisiblesection.

Also, please note the redundancy in that I have to pass my string twice: once for the header, and once for the table of contents.

Request

Please, include a complete minimum working example to your solution.

MINIMUM WORKING EXAMPLE

\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}

2 Answers 2

0

I'm not entirely sure of the effects you are seeking, but the following works around some of the toc and header problems I found running your MWE and also deals with some redundancy. The issue of headers depends on where the page breaks -- you will get the last header declared on the page, so I added some latin to illustrate. The result is a few pages so I am leaving out images.

    \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}
3
  • Its true that the code of @John does many of the things that are required. However, in order to update the header correctly each and every time, its might still be required to manually insert a \clearpage between items. Commented May 27, 2018 at 16:48
  • @Michael Levy -- The header is whatever was the last \rhead on the page. So, the 1st and 2nd Exhibits start on pg 2, so the 2nd is the header on that page. Page 3 has this as well, because I moved the 3rd exhibit's \invisiblesection to land on the 4th page, which has got the 3rd exhibit as header. You can change the content of the code I posted to see how these headers move around depending upon where the exhibit starts. If you want the Exhibits on their own individual pages with their own individual headers, you will require a \clearpage between each.
    – John
    Commented May 27, 2018 at 17:33
  • Yes, your last comment makes that clear. For what its worth, the code you produced has an unintended consequence: tex.stackexchange.com/questions/433649/… Commented May 27, 2018 at 17:36
0

Here is what I ended up going with:

This code meets all my requirements. Though I could not figure out how to avoid the need to manually add \clearpage commands after includegraphics commands.

MINIMUM WORKING EXAMPLE

\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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .