3

My document is in French. I added appendices to the document, appendices use \chapter{}, hence I would like that these chapters to be names "Annexe" (which is the French word for "Appendice" in table of content.

But I my current document I don't manage to do that.

I expect it has to do with the cutomization of my table of content. Which I want to keep as well.

working example:

%Preamble
\documentclass[a4paper,french,oneside,openright]{book}

\usepackage[french]{babel}

\usepackage{hyperref}
\hypersetup{
    colorlinks=true,
    linkcolor=blue,
    filecolor=magenta,      
    urlcolor=cyan
}
\usepackage{lastpage}
\usepackage{lipsum}

\usepackage{tocloft,calc}
\renewcommand{\cftchappresnum}{Chapitre }
\AtBeginDocument{\addtolength\cftchapnumwidth{\widthof{\bfseries Chapitre }}}


\usepackage{datetime}

\usepackage[toc]{appendix}
\begin{document}

    \tableofcontents
    
\part{main}
    \chapter{Lorem ipsum}
    \lipsum[1-2]
    

\renewcommand{\appendixname}{Annexe}
\renewcommand{\appendixtocname}{Annexe}
\renewcommand{\appendixpagename}{Annexe}

\part{appendices}
\begin{appendices}
    \chapter{Some appendice}
    \lipsum[1-2]
    \chapter{Some appendice}
    \lipsum[1-2]
\end{appendices}

\end{document}

I know that this question has been closed and pointing to \chaptername is used even for appendix chapters in ToC but that didn't help me at all. Another answer is marked as solutions under this question. That one helped me solve the issue.

5
  • See the babel manual you need to add your name changes to the language it self somthing like \addto\captionsfrench{.....} where you supply the ... part – daleif Jun 15 at 8:40
  • 5
    Does this answer your question? \chaptername is used even for appendix chapters in ToC – Markus G. Jun 15 at 9:02
  • Your question has an answer here: tex.stackexchange.com/q/56839/118712 (BTW: The approach from daleif will not work with the renewcommand of cftchappresnum.) – Markus G. Jun 15 at 9:03
  • 3
    Unrelated: It is generally advisable to load the hyperref package LAST, unless another package specificially and explicitly requires being loaded after hyperref. – Markus G. Jun 15 at 9:05
  • Yet another question "closed" as "Already has an answer" ... then pointing to an answer that didn't help me solve the problem... I'm not sure how I should react... I have already marked an answer as solution in this case anyway... – TTT Jun 15 at 22:37
2

You can tweak the \addcontentsline macro as it is done in the appendix package (when using the titletoc option).

\documentclass[a4paper,french,oneside,openright]{book}

\usepackage{lastpage}
\usepackage{lipsum}
\usepackage{tocloft,calc}

% a command to tweak \addcontentsline for chapters
\newcommand{\chapitretocentry}{%
    \let\oldacl=\addcontentsline % save original definition
    \def\addcontentsline##1##2##3{% (re)define \addcontentsline
        \def\tempa{##1}\def\tempb{toc}% store ##1 and toc in temp vars
        \ifx\tempa\tempb % check if they are "equal"
            \def\tempa{##2}\def\tempb{chapter}%
            \ifx\tempa\tempb % check if ##2 is "equal" to chapter
                \oldacl{##1}{##2}{\chaptername\space ##3}% apply original \addcontentsline adding \chaptername
% in the other cases, just apply the original \addcontentsline
            \else
                \oldacl{##1}{##2}{##3}%
            \fi
        \else
            \oldacl{##1}{##2}{##3}%
        \fi
    }
}
\AtBeginDocument{\chapitretocentry} % invoke the command at the beginning

\usepackage{datetime}
\usepackage[titletoc]{appendix}
\usepackage[french]{babel}

\usepackage{hyperref}
\hypersetup{
    colorlinks=true,
    linkcolor=blue,
    filecolor=magenta,      
    urlcolor=cyan
}
\begin{document}
    
    \tableofcontents
    
    \part{main}
    \chapter{Lorem ipsum}
    \lipsum[1-2]
    
    
    \renewcommand{\appendixname}{Annexe}
    \renewcommand{\appendixtocname}{Annexes}
    \renewcommand{\appendixpagename}{Annexe}
    
    \part{Annexes}
    \let\addcontentsline=\oldacl % reset the original def
    \begin{appendices}
        \chapter{Some appendice}
        \lipsum[1-2]
        \chapter{Some appendice}
        \lipsum[1-2]
    \end{appendices}
    
\end{document}

enter image description here

3
  • This looks like it works when I copy your whole answer, but I tried to "inject it" in my example and it didn't work... trying again... – TTT Jun 15 at 9:32
  • Ok, got it working. Currently I'm half-lost in what it does but it works, thanks. – TTT Jun 15 at 9:41
  • @TTT I added some comments to explain briefly the code – NBur Jun 15 at 9:52
4

You have to tell LaTeX that when processing the appendices in the TOC, \chaptername should become \appendixname.

The setting will disappear as soon as the TOC has been typeset, because this happens inside a group.

\documentclass[a4paper,french,oneside,openright]{book}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}

\usepackage{hyperref}
\hypersetup{
    colorlinks=true,
    linkcolor=blue,
    filecolor=magenta,      
    urlcolor=cyan
}
\usepackage{lastpage}
\usepackage{lipsum}
\usepackage{tocloft,calc}
\usepackage{datetime}
\usepackage[toc]{appendix}

\renewcommand{\cftchappresnum}{\chaptername\ }
\AtBeginDocument{\addtolength\cftchapnumwidth{\widthof{\bfseries\chaptername }}}
\AddToHook{env/appendices/begin}{%
  \addtocontents{toc}{\let\string\chaptername\string\appendixname}%
}

\begin{document}

\tableofcontents
    
\part{main}

\chapter{Lorem ipsum}

\lipsum[1-2]
    
\part{appendices}
\begin{appendices}

\chapter{Some appendice}

\lipsum[1-2]

\chapter{Some appendice}

\lipsum[1-2]

\end{appendices}

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.