1

I'm writing my thesis and Appendices have to say “Anexo” before chapter letter. I tried using \usepackage[titletoc]{appendix}, but it didn't work. Also tried to comment command from titlesec and titletoc, but even with this I couldn't make it work.

Here is my MWE:

\documentclass[12pt]{report}

\usepackage[utf8]{inputenc} \usepackage[spanish]{babel} % pone el idioma en español

\usepackage[titletoc]{appendix}

\renewcommand{\appendixname}{Anexos} \addto\captionsspanish{\renewcommand{\appendixname}{Anexo}}

\usepackage{titlesec} \titleformat{\chapter}[display] {\large\bfseries\centering}{\MakeUppercase\chaptertitlename\ \thechapter}{1em}{\MakeUppercase}{} % Cambios en Capitulo

\titleformat{\section}{\large\bfseries}{\thesection.}{1em}{}

\usepackage{titletoc}

\titlecontents{chapter}[0em] {\vspace{0em}} {\normalfont\normalsize\contentslabel[\thecontentslabel.]{2em}\uppercase} {\hspace{-2em}\uppercase} {\titlerule[.75em]{.}\contentspage}

\titlecontents{section}[2em] {\vspace{0em}} {\normalfont\normalsize\contentslabel[\thecontentslabel.]{2em}} {\hspace{-2em}} {\titlerule[.75em]{.}\contentspage}

\usepackage[]{hyperref} % Opción para que aparesca el indice en el pdf

\begin{document}

\tableofcontents

\chapter{text}

\section{text}

\appendix

\chapter{text} \end{document}

The output is shown in the picture, and I need to print "Anexo A":

enter image description here

Edit 1: I tried adding some code to manually add "Anexo #:" to the titletoc code, without good results:

\appendix

\titlecontents{chapter}[0em] {\vspace{0em}} {\normalfont\normalsize\contentslabel[Anexo \thecontentslabel:]{2em}\uppercase} {\hspace{-2em}\uppercase} {\titlerule[.75em]{.}\contentspage}

\chapter{text} \chapter{text}

The result:

enter image description here

Is there a way to move the chapter title to the right, to make this solution viable?

JJP
  • 81
  • Somewhat related: https://tex.stackexchange.com/questions/406388/formatting-table-of-contents-so-it-has-chapter-in-front-of-every-chapter-and-app Actually, one could simplify the solution by using \chapapp instead of \chaptername and/or \appendixname. – John Kormylo Sep 23 '23 at 15:50
  • I tried modifying that solution, but I couldn't get it running to only change the appendix. Could you give my an example on how to use it? – JJP Sep 25 '23 at 13:15
  • 1
    I stay away from both titlesec and titiletoc. – John Kormylo Sep 26 '23 at 03:27
  • @JohnKormylo Not just me? I thought I was the only one who couldn't get on with titlesec. (Haven't really experimented with titletoc.) – cfr Sep 26 '23 at 04:17

1 Answers1

4

Borrowing heavily from two other answers on the site

The following seems to do what you want:

\documentclass[12pt]{report}
\usepackage{etoolbox}
\usepackage[spanish]{babel} % pone el idioma en español
\newlength{\toclabelwidth}
\setlength{\toclabelwidth}{2em}
\addto\captionsspanish{\renewcommand{\appendixname}{Anexo}}
\usepackage{titlesec,titletoc}
\setcounter{tocdepth}{3}
\titleformat{\chapter}[display] 
{\large\bfseries\centering}{\MakeUppercase\chaptertitlename\ \thechapter}{1em}{\MakeUppercase}{} % Cambios en Capitulo
\titleformat{\section}{\large\bfseries}{\thesection.}{1em}{}
\makeatletter
\appto\appendix{\addtocontents{toc}{\setlength{\toclabelwidth}{5em}\gdef\protect\@chapapp{\protect\appendixname}}}
\appto\contentsfinish{\gdef\@chapapp{\chaptername}}
\makeatletter
\usepackage{titletoc}
\titlecontents{chapter}
[\toclabelwidth]
{\normalfont\renewcommand\chaptername{}}
{\contentslabel[\@chapapp~\thecontentslabel\quad]{\toclabelwidth}\MakeUppercase}
{}
{\titlerule*[.75em]{.}\contentspage}
\makeatother
\titlecontents{section}
[4em]
{}
{\normalfont\normalsize\contentslabel[\thecontentslabel.]{2em}}
{\hspace*{-2em}}
{\titlerule*[.75em]{.}\contentspage}

\begin{document}

\tableofcontents

\chapter{text}

\section{text} \subsection{text}

\appendix

\chapter{text} \section{appendix} \subsection{appendix} \end{document}

output of code

Alan Munn
  • 218,180
  • Thanks for your answer! I will try it to see how it works and how to implement it. (don't want to just copy and paste) – JJP Oct 02 '23 at 14:40
  • Is there a way to only move “Anexo A TEXT” to the right in TOC, and not all chapters ("1 TEXT”)?. The solution work greats, but I feel that for normal chapters ("1 TEXT”) it takes a lot of the title space. I have TOCDEPTH to 3 (subsubsection in my thesis document), so more space would be great. – JJP Oct 02 '23 at 15:20
  • 1
    I don't really see a pleasing way to solve that problem, you don't want the Anexo part to overlap the left margin. Would you want the regular chapter numbers to line up with the left margin and then have a small indent after? – Alan Munn Oct 02 '23 at 16:06
  • I would prefer the Chapter number aligned at left margin, while also having "Anexo A" aligned to the left margin. In \titlecontents{chapter}[4.9em], I was expecting a way to change "4.9em", something like: if (anexo) -> 4.9em, else -> othervalue. – JJP Oct 02 '23 at 16:13
  • 1
    I've updated the answer so that only the appendix has the larger margin. – Alan Munn Oct 02 '23 at 16:21
  • Thanks for your time! That was exactly what I needed! :) – JJP Oct 02 '23 at 16:46