TeX - LaTeX Asked by Old Fish on June 14, 2021
I am converting a complex markdown document to pdf using pandoc. And need to add some formatting to it during the conversion. So I cannot have the styling and the data at the same place, means, I cannot do a target style assignment with something like thispagestyle
. How can I make a general conditional headersfooters for some pages?
What I can think of now for removing header on the pages with table of content is below. It is not working for some reason.
(for this case fancyhead[C]{textsl{leftmark}}
would set the word ‘CONTENTS’ in the header of this page).
usepackage{fancyhdr}
usepackage{ifthen}
pagestyle{fancy}
...
some general header/footer style
...
fancyheadinit{
ifthenelse{equal{leftmark}{Contents} OR equal{leftmark}{CONTENTS}}
{
fancyhf{}
{fancyhead[C]{textsl{rightmark}}}
renewcommand{headrulewidth}{0pt}
}
}
Here is a thought process for a solution:
Capture the chapter name/title in something like chaptername
by redefining chapter
.
Look up with the chapter name of the ToC actually looks like in the class you use (I've looked at book.cls
and report.cls
which are both the same; it may be different for other document classes). Store that in ToCchaptername
.
Compare the current chaptername
against ToCchaptername
to condition on what you (don't) set.
The following example does that, leaving the ToC without any heading at all.
documentclass{book}
usepackage{fancyhdr,lipsum}
% Capture the chapter name/title in chaptername
makeatletter
letoldchapterchapter
RenewDocumentCommand{chapter}{s o m}{%
cleardoublepage
IfBooleanTF{#1}
{oldchapter*{#3}defchaptername{#3}}% chapter*{...}
{IfValueTF{#2}
{oldchapter[#2]{#3}defchaptername{#2}}% chapter[..]{...}
{oldchapter{#3}defchaptername{#3}}}% chapter{...}
markright{}% Clear possible section mark left by ToC
}
% Taken from https://www.tug.org/svn/texlive/trunk/Master/texmf-dist/tex/latex/base/book.cls?view=co
defToCchaptername{contentsname @mkboth{%
MakeUppercasecontentsname}{MakeUppercasecontentsname}}
makeatother
pagestyle{fancy}
fancyheadinit{%
fancyhf{}% Remove header/footer
renewcommand{headrulewidth}{0pt}% Remove header rule
% Compare chapter title/name to that of ToC
ifxchapternameToCchaptername
% What to do when you're in the ToC
else
% What to do when you're not in the ToC
fancyhead[C]{textsl{rightmark}}
fi
}
begin{document}
sloppy
tableofcontents
chapter{First chapter}lipsum[1-10]
section{First section}lipsum[11-20]
section{Second section}lipsum[21-30]
section{Third section}lipsum[31-40]
section{Final section}lipsum[41-50]
chapter{Second chapter}lipsum[1-10]
section{First section}lipsum[11-20]
section{Second section}lipsum[21-30]
section{Third section}lipsum[31-40]
section{Final section}lipsum[41-50]
chapter{Third chapter}lipsum[1-10]
section{First section}lipsum[11-20]
section{Second section}lipsum[21-30]
section{Third section}lipsum[31-40]
section{Final section}lipsum[41-50]
chapter{Final chapter}lipsum[1-10]
section{First section}lipsum[11-20]
section{Second section}lipsum[21-30]
section{Third section}lipsum[31-40]
section{Final section}lipsum[41-50]
end{document}
A small edge-case had to be considered (because you're setting rightmark
) since the ToC sets both left/right header to be contentsname
. So, if the first chapter after the ToC has a long introduction (spilling onto a second page) before the first section
(and therefore rightmark
) is set, you could be in a new chapter and set the word contentsline
as part of rightmark
. As such, we clear the rightmark
when we call chapter
(via markright{}
).
Answered by Werner on June 14, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP