# epigraph plus standalone gives unexpected results

I want to write a long document therefore I will split it in several files and use the package standalone to import them. I also want to add epigraphs before the chapters names, thus I use the package epigraph and the command \epigrahhead as recommended in the documentation and in the following example.

But with this combination, the last epigraph printed is the one of the first chapter.

Here is an example:

\documentclass[a4paper]{book}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{epigraph}
\usepackage{standalone}

\usepackage{filecontents}

\begin{filecontents}{chapter2.tex}
\documentclass[a4paper]{book}

\usepackage[T1]{fontenc}
\usepackage{epigraph}

\begin{document}

\chapter{Chapter title}
\epigraph{\itshape Computer programming is an art, because it applies
accumulated knowledge to the world, because it requires skill and ingenuity,
and especially because it produces objects of beauty.
}{Donald Knuth, \textit{"Computer Programming as an Art". Communications of the ACM, Volume 17, Issue 12, dl.acm.org., December 1974}}
}

\section{section title}
Text again.

\chapter{Last chapter}
\epigraph{Last epigraph}{ Probably someone}
}

\section{Last section}
Last text.

\end{document}
\end{filecontents}

\begin{document}

\chapter{CHAPTER}
\epigraph{Poor cultural foundations were a significant
driver of the 2008–2009 financial crisis(in common
with many past crises). Behaviors that do not meet
banks’ desired values and conduct continue to be a
problem.}{Group of Thirty (2015), p. 18}
}

\section{SECTION}
Text.

\input{./chapter2.tex}

\end{document}


So I wonder why there is this unexpected results ?

### EDIT

Thanks to the answer given by egreg, it seems that commenting the first epigraph only implies a different behaviour:

    \chapter{CHAPTER}
%   \epigraph{Poor cultural foundations were a significant
%       driver of the 2008–2009 financial crisis(in common
%       with many past crises). Behaviors that do not meet
%       banks’ desired values and conduct continue to be a
%       problem.}{Group of Thirty (2015), p. 18}
%}

\section{SECTION}
Text.


The problem is that what's typeset when standalone is in force, like when you \input{chapter2.tex}, is done in a group.

With epigraph, the text of the epigraph is stored in the temporary control sequence @epitemp and some juggling with the page style is done.

What happens with your very short final chapter is that the input file ends before the chapter page is shipped out, so the group is ended and the value of \@epitemp restored to what it was before \input. Hence you get the last epigraph before \input.

Add \clearpage at the end of the short chapter.

\begin{filecontents}{chapter2.tex}
\documentclass[a4paper]{book}

\usepackage[T1]{fontenc}
\usepackage{epigraph}

\begin{document}

\chapter{Chapter title}
%   \epigraph{\itshape Computer programming is an art, because it applies
%              accumulated knowledge to the world, because it requires skill and ingenuity,
%              and especially because it produces objects of beauty.
%   }{Donald Knuth, \textit{Computer Programming as an Art''.
%     Communications of the ACM, Volume 17, Issue 12, dl.acm.org., December 1974}}
%}

\section{section title}
Text again.

\clearpage

\chapter{Last chapter}
\epigraph{Last epigraph}{Probably someone}
}

\section{Last section}
Last text.

\clearpage % <----- HERE

\end{document}
\end{filecontents}

\documentclass[a4paper]{book}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{epigraph}
\usepackage{standalone}

\begin{document}

\chapter{CHAPTER}
\epigraph{Poor cultural foundations were a significant
driver of the 2008–2009 financial crisis(in common
with many past crises). Behaviors that do not meet
banks’ desired values and conduct continue to be a
problem.}{Group of Thirty (2015), p. 18}
}

\section{SECTION}
Text.

\input{./chapter2.tex}

\end{document}


• I was trying your solution and I commented the whole \epigraphead` block which made disappear the last epigraph. Isn't it the same kind of problem ? – R. N Jan 14 at 15:02
• @R.N If you remove the epigraph, what do you expect? – egreg Jan 14 at 15:07
• Sorry, I did not express myself well, I remove only the first epigraph and that made disappear the last one. So I expect to have the second and third epigraph printed. I will edit my question to make the clearer the situation. – R. N Jan 14 at 15:26
• @R.N Same problem. Short chapter. LaTeX cannot realize that an epigraph is needed. – egreg Jan 14 at 15:29
• Ok but it is possible to solve this kind of situation also ? – R. N Jan 14 at 15:34