# Using titletoc with ACM acmart style

The recent acmart style (try here if that link is broken) doesn't seem to work properly with titletoc, producing an unwanted string of section names at the beginning of the table of contents:

I'm looking for output similar to that shown in the answer to this question, which I can get by replacing acmart by article:

I have looked at other table-of-contents solutions, but I particularly like the output of titletoc, so I would like to get that working if possible.

Here's the code:

\documentclass{acmart}

\usepackage{appendix}
\usepackage{titletoc}

\newcommand\DoToC{%
\startcontents
\printcontents{}{2}{\textbf{Contents}\vskip3pt\hrule\vskip5pt}
\vskip3pt\hrule\vskip5pt
}

\begin{document}

\DoToC

\section{My First Section}
\section{My Second Section}

\begin{appendices}
\section{My First Appendix}
\DoToC
\subsection{My First Subsection}
\subsection{My Second Subsection}

\section{My Second Appendix}
\DoToC
\subsection{My First Subsection}
\subsection{My Second Subsection}
\end{appendices}

\end{document}

• Where is that class fileacmart.cls ?
– M S
Mar 7 '17 at 10:26
• See link at beginning of question.
– Roly
Mar 7 '17 at 10:27
• Yes. Not Working and opening here.
– M S
Mar 7 '17 at 10:28
• Weird. Opens for me fine. Added a link to the CTAN package.
– Roly
Mar 7 '17 at 10:36

The problem is, acmart loads hyperref. Normally hyperref redefines some titletoc internals to work. However, since you now load titletoc after hyperref, it does not have a chance to do so.

This means you need to redefine them yourself.

This works:

\documentclass{acmart}
\usepackage{appendix}
\usepackage{titletoc}
\makeatletter
\Hy@MakeCurrentHrefAuto{#1*}%
\edef\ttl@Hy@saveanchor{%
\noexpand\hyper@anchorstart{\@currentHref}%
\noexpand\hyper@anchorend
\def\noexpand\ttl@Hy@SavedCurrentHref{\@currentHref}%
\noexpand\ttl@Hy@PatchSaveWrite
}%
}%
}%
\def\ttl@Hy@PatchSaveWrite{%
\begingroup
\toks@\expandafter{\ttl@savewrite}%
\edef\x{\endgroup
\def\noexpand\ttl@savewrite{%
\let\noexpand\@currentHref
\noexpand\ttl@Hy@SavedCurrentHref
\the\toks@
}%
}%
\x
}%
\def\ttl@Hy@refstepcounter#1{%
}%
\refstepcounter{#1}%
}%
\def\ttl@gobblecontents#1#2#3#4{\ignorespaces}%
\makeatother

\newcommand\DoToC{%
\startcontents
\printcontents{}{2}{\textbf{Contents}\vskip3pt\hrule\vskip5pt}
\vskip3pt\hrule\vskip5pt
}

\begin{document}

\DoToC

\section{My First Section}
\section{My Second Section}

\begin{appendices}
\section{My First Appendix}
\DoToC
\subsection{My First Subsection}
\subsection{My Second Subsection}

\section{My Second Appendix}
\DoToC
\subsection{My First Subsection}
\subsection{My Second Subsection}
\end{appendices}

\end{document}