# Unravel the \writecontentsline mystery

Trying to program a macro that is similar to \adcontentsline: Applying \immediate\write to, say ,

\frameEntry{\insertframenumber}{\inserframetitle \}{\thepage}


records the wrong page number. Conversely, if the \write is postponed to the time the page is shipped, then the wrong \insertpagenumber and the wrong \insertpagetitle are recorded.

More context: I work on writing my own little set of macros for table of contents., with the purpose of generating an edition of set of some hundreds of slides in article mode. The challenge is to produce a list of frames in the style of table of contents, right after each \subsection.

Packages such as minitoc and titletoc did not work. The former since it refuses to produce a table of contents and the latter because it does not play well with hyperref.

• you should be able to use \protected@write that expands everything except \thepage at the point of use and \thepage as the page is shipped out. – David Carlisle Sep 23 '14 at 18:32
• have you tried etoc ? – user4686 Sep 23 '14 at 19:38
• Not really. That is to say, I tried reading the manual, more than once. Could not really figure how I should use the package. I would appreciate help in doing it. – Yossi Gil Sep 24 '14 at 4:36
• apologies about my misguided advice, I completely overlooked your beamer tag and mentions of frames. The beamer class completely rewrites the format of the .toc file, hence etoc is incompatible with this class. – user4686 Sep 24 '14 at 6:12
• however, using it with \documentclass{article}\usepackage{beamerarticle} is possible. Is this what you mean? a mwe would have helped. – user4686 Sep 24 '14 at 6:40

Package etoc is incompatible with the beamer class.

However, if beamer is used in an article mode, i.e., with the article class and the beamerarticle package, then etoc is applicable:

\documentclass{article}
\usepackage{beamerarticle}
\usepackage{etoc}
% section=1, subsection=2, subsubsection=3
\etocsetlevel {beamerframe}{6}% dummy, a priori invisible, level
\etocsettocdepth {all}

% Earlier provisory code by jfbu
% \let\oldframetitle \frametitle
% \renewcommand\frametitle [1]{%
%              \etoctoccontentsline{beamerframe}{#1}%
%              \oldframetitle {#1}%
% }
%%%%

% Better code by Yossi Gil
% Override action when frame title is encountered:
\setbeamertemplate{frametitle}{%
\noindent\emph{\insertframesubtitle}\par
}
%%%%

% Command to list frames in a sub-section:
\newcommand\listofframesinsubsection {\begingroup
% we are going to list one frame per line, with dots up to the page number,
% this is the default etoc design for subsections, we thus need to set the
% level of a beamerframe to 2. But then we need to artificially move up the
% leve of subsection so that \localtableofcontents does see the beamerframes
% as sub levels of a subsection
\etocsetlevel {subsection}{1}% artificially pretending subsections are
% sections  one up the level
\etocsetlevel {beamerframe}{2}% pretending beamerframes are subsections
\etoctoclines % use the styles defined by \etocsetstyle, or here, as we
% didn't make any use of \etocsetstyle, just defaults
% to the package default styles (which are convenient for
% us here.)
\etocsettocstyle {\noindent Frames in this subsection:\par}{}%
\etocsetnexttocdepth {beamerframe}%
\localtableofcontents
\endgroup % clean up all our mess for the next \localtableofcontents  not to
% be affected and proceed in compatibility mode with the same
% default design as in article class
}

\begin{document}

\etocsetnexttocdepth {subsection}
\tableofcontents

\section{Some frames}

%\etocsettocstyle{\subsection*{Local contents:}}{}
\etocsettocstyle {}{}
\etocsetnexttocdepth {subsection}
\localtableofcontents

\subsection {first subsection}
\listofframesinsubsection

\begin{frame}\frametitle{AHK}
Ah Ah
\end{frame}

\begin{frame}\frametitle{AHJ}
Oh Oh
\end{frame}

\subsection {second subsection}
\listofframesinsubsection

\begin{frame}\frametitle{HBZ}
Ah Ah
\end{frame}

\begin{frame}\frametitle{HBW}
Oh Oh
\end{frame}

\section{More frames}

%\etocsettocstyle{\subsection*{Local contents:}}{}
\etocsettocstyle {}{}
\etocsetnexttocdepth {subsection}
\localtableofcontents

\subsection {third subsection}
\listofframesinsubsection

\begin{frame}\frametitle{BHK}
Ah Ah
\end{frame}

\begin{frame}\frametitle{BHJ}
Oh Oh
\end{frame}

\subsection {fourth subsection}
\listofframesinsubsection

\begin{frame}\frametitle{BBZ}
Ah Ah
\end{frame}

\begin{frame}\frametitle{BBW}
Oh Oh
\end{frame}

\end{document}


(the images have been re-generated to reflect the insertion of the frame number added in updated code)

Note: in the code above, the frames declare a paragraph; if the only paragraphs encountered in the subsections are associated in such a way with frames, it is possible to simplify the preamble, there is no need to define a beamerframe sectioning level for etoc. Here is such a streamlined preamble (if the document has already been compiled with the earlier preamble, one should trash the auxiliary files, or just compile twice, ignoring error on first compilation).

\documentclass{article}
\usepackage{beamerarticle}
\usepackage{etoc}
% section=1, subsection=2, subsubsection=3
\etocsettocdepth {all}

\setbeamertemplate{frametitle}{%
\noindent\emph{\insertframesubtitle}\par
}

% Command to list frames in a sub-section:
\newcommand\listofframesinsubsection {\begingroup
\etocsetlevel {subsection}{1}% pretending subsections are sections
\etocsetlevel {paragraph}{2}% pretending paragraphs are subsections
\etoctoclines % allows to use the package default styles for subsections
\etocsettocstyle {\noindent Frames in this subsection:\par}{}%
\etocsetnexttocdepth {paragraph}%
\localtableofcontents
\endgroup
}


[YG:]

The above commands will not work in any mode other than article. For this reason it is probably best to protect the above in \mode{most of the above preamble}.

Another best practice is to use two separate containing files, for the article and the slides mode. Both this files should \input the files where the actual frames are. If this is what you do, then there is no need for such a protection.

• I think it is better to encapsulate the commands in \mode<article>{...}. Also, \setbeamertemplate{frametitle}{% \paragraph{\insertframenumber.~\insertframetitle}\\ \noindent\emph{\insertframesubtitle}\par \etoctoccontentsline{frame}{\insertframenumber.~\insertframetitle}% } might be slightly better than overriding \frametitle. – Yossi Gil Sep 24 '14 at 12:42
• sadly I am quite ignorant in beamer matters as you realized from my slow understanding that beamer in article mode meant beamerarticle package in another class than beamer. I find this wording in the beamer manual quite disputable, if we are in article class, we are in article class, not beamer class (hence my etoc is not compatible with beamer class which you have edited). Please do edit further if you feel like it my answer (explaining it is your addition), adding at the end the correct formulation. I patched \frametitle from lack of knowledge, I am glad there is a better way. – user4686 Sep 24 '14 at 12:49
• Would it be OK if I edit your answer to reflect the changes I suggest? – Yossi Gil Sep 24 '14 at 13:27
• @YossiGil absolutely OK, please do. – user4686 Sep 24 '14 at 14:11
• @YossiGil I pay attention to it only now, but your code allows to drop entirely my \etoctoccontentsline thing. Indeed, your \frametitle does a paragraph. These go to the toc file. Thus, replace beamerframe by paragraph in the command \listofframesinsubsection, suppress the \etoctoccontentsline thing and the \etocsetlevel{beamerframe}{6} and it will all work. – user4686 Sep 25 '14 at 21:05