[Tex/LaTex] Separate toc for appendix

appendicesarticletable of contents

I am struggeling with creating a seperate toc for my appendix.

This is what I currently have:

Contents
1 first section……1
2 second section…..2
List of appendices…5

This is what I'd like to achieve:

Contents
1 first section……1
2 second section…..2
List of appendices…5

plus a seperate toc for my appendix including all sections:

List of appendices
I My first appendix……5
II My second appendix…..6

This is my code so far:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\usepackage{lipsum}

\usepackage[toc,page]{appendix}

% as per https://tex.stackexchange.com/questions/100479/label-appendix-as-appendix-i-ii-iii-rather-than-appendix-a-b-and-c
\let\oldappendix\appendices
\renewcommand{\appendices}{%
  \clearpage
  \oldappendix
  \renewcommand{\thesection}{\Roman{section}}
  \addtocontents{toc}{\protect\setcounter{tocdepth}{0}}
  }%

% use custom names for appendices
\renewcommand{\appendixtocname}{List of appendices}
\renewcommand{\appendixpagename}{List of appendices}

\begin{document}

\tableofcontents
\section{first section}
\lipsum
\section{second section}
\lipsum

\begin{appendices}
    \section{My first appendix}
    \lipsum
    \section{My second appendix}
    \lipsum
\end{appendices}

\end{document}

Your help is highly appreciated. I've tried solving my issue with minitoc, however I couldn't achieve what I exactly wanted. I really apologize if this is a duplicate question, but I couldn't find answers for my specific problem.

Best Answer

enter image description here

The separate Toc for the appendix entries can be achieved by letting an .app (for example) file handle point to .toc, i.e. at a certain point of document stopping to write to the .toc file and everything meant for the .toc enters the .app file.

I have defined a \listofappendices macro, that reuses \tableofcontents by renaming \contentsname and applying \@starttoc{app} instead of \@starttoc{toc}.

Please note, that any writting attempt to the .toc will finally be placed in the .app file, so if this is not desired, the file handle must be restored at the end of appendices; this is not yet done.

\documentclass{article}
\usepackage{lipsum}
%\usepackage[utf8]{inputenc} % Not needed since TL 2018
\usepackage{hyperref}
\usepackage[page]{appendix}


% as per https://tex.stackexchange.com/questions/100479/label-appendix-as-appendix-i-ii-iii-rather-than-appendix-a-b-and-c

% use custom names for appendices

\renewcommand{\appendixtocname}{List of appendices}
\renewcommand{\appendixpagename}{Appendices}

\makeatletter
\let\oldappendix\appendices

\renewcommand{\appendices}{%
  \clearpage
  \renewcommand{\thesection}{\Roman{section}}
  % From now, everything goes to the app - file and not to the toc
  \let\tf@toc\tf@app
  \addtocontents{app}{\protect\setcounter{tocdepth}{1}}
  \immediate\write\@auxout{%
    \string\let\string\tf@toc\string\tf@app^^J
  }
  \oldappendix
}%



\newcommand{\listofappendices}{%
  \begingroup
  \renewcommand{\contentsname}{\appendixtocname}
  \let\@oldstarttoc\@starttoc
  \def\@starttoc##1{\@oldstarttoc{app}}
  \tableofcontents% Reusing the code for \tableofcontents with different \contentsname and different file handle app
  \endgroup
}

\makeatother


\begin{document}

\tableofcontents

\listofappendices



\section{first section}
\lipsum
\section{second section}
\lipsum

\section{Third section}
\lipsum




\begin{appendices}
    \section{My first appendix}
    \lipsum
    \section{My second appendix}
    \lipsum

    \section{My third appendix}
    \lipsum

\end{appendices}

\end{document}

Update Version with restoring the possibility to write to the original ToC after the appendices:

\documentclass{article}
\usepackage{lipsum}
%\usepackage[utf8]{inputenc} % Not needed since TL 2018
\usepackage[page]{appendix}
\usepackage{etoolbox}
\usepackage{hyperref}


% as per https://tex.stackexchange.com/questions/100479/label-appendix-as-appendix-i-ii-iii-rather-than-appendix-a-b-and-c

% use custom names for appendices

\setcounter{tocdepth}{3}
\renewcommand{\appendixtocname}{List of appendices}
\renewcommand{\appendixpagename}{Appendices}

\makeatletter
\let\oldappendix\appendices

\g@addto@macro\tableofcontents{%
  % Store the current toc file for later usage
  \let\tf@toc@orig\tf@toc
}
\renewcommand{\appendices}{%
  \clearpage
  \renewcommand{\thesection}{\Roman{section}}
  % From now, everything goes to the app - file and not to the toc
  \let\tf@toc\tf@app
  \addtocontents{app}{\protect\setcounter{tocdepth}{1}}
  \immediate\write\@auxout{%
    \string\let\string\tf@toc\string\tf@app
  }
  \oldappendix
}%

\g@addto@macro\endappendices{%
  % Switch back to the old toc file handle
  \let\tf@toc\tf@toc@orig
  \immediate\write\@auxout{%
    \string\let\string\tf@toc\string\tf@toc@orig
  }%
}  

\newcommand{\listofappendices}{%
  \begingroup
  \renewcommand{\contentsname}{\appendixtocname}
  \let\@oldstarttoc\@starttoc
  \def\@starttoc##1{\@oldstarttoc{app}}
  \tableofcontents% Reusing the code for \tableofcontents with different \contentsname and different file handle app
  \endgroup
}

\makeatother


\begin{document}

\tableofcontents

\listofappendices



\section{first section}
\lipsum
\section{second section}
\lipsum

\section{Third section}
\lipsum




\begin{appendices}
    \section{My first appendix}
    \lipsum
    \section{My second appendix}
    \lipsum

    \section{My third appendix}
    \lipsum

\end{appendices}

\section{A section after the appendices}

\end{document}

enter image description here