I was trying to define a new environment in which I needed to have an argument in the end code. Thanks to this post, I got that it is not possible without a trick. That is why I defined a command in the beginning containing the argument to use it in the end, but in vain... Can someone explain me why and/or fix this?
\newenvironment{newappendix}[2]%
{\def\headername{#1}%
\phantomsection%
\refstepcounter{section}%
\addcontentsline{toc}{section}{Appendix \thesection~- #1}%
\label{app:#2}}
{\fancyhead[R]{Appendix \thesection~- \headername}}
Without the command \headername
on the last line it works fine but I need this argument here so... What to do?
EDIT Here is a MWE (by uncommenting the last line of the end of the definition of the environment you should get an error...
\documentclass{report}
\usepackage[demo]{graphicx}
\usepackage{hyperref}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead[L]{{\bfseries Appendices}}%
\setcounter{section}{0}%
\renewcommand{\thesection}{\Alph{section}}%
\newenvironment{newappendix}[2]%
{\def\headername{#1}%
\phantomsection%
\refstepcounter{section}%
\addcontentsline{toc}{section}{Appendix \thesection~- #1}%
\label{app:#2}}
{\fancyhead[R]{Appendix \thesection~-}}% \headername}}
\begin{document}
\begin{newappendix}{first}{1}
\includegraphics[width=12cm,height=15cm]{test}
\end{newappendix}
\end{document}
\def\headername{#1}
to\gdef\headername{#1}
.\rightmark
?\mark
system where the page breaker informs the heading code automatically the first and last\mark
on the page, marks are typically set by section headings so they can be picked up by the page head. If you set th epage head "by hand" as here it will do the wrong thing if you have multiple sections on a page or if you are unlucky and a page break happens close to a section head.