1

I mean like having the table of content like:

Chapter X. Introduction

Chapter X. Abstract

Chapter X. Greetings

Where "X" is the automatic number put by LaTex.

All I've achieved is the following output, but I added "Capítulo 1." manually

Issue

Here's all packages I'm importing to my project, and my \documentclass

\documentclass[12pt, letterpaper, twoside]{book}
\usepackage[utf8]{inputenc}
\usepackage[spanish]{babel}
\usepackage[top=25mm, bottom=25mm,right=25mm,left=30mm]{geometry}
\usepackage{ragged2e}
\usepackage{graphicx}
\usepackage[table]{xcolor}
\usepackage{import}
\usepackage{enumerate}
\usepackage{tabularx,ragged2e,booktabs,caption}
\usepackage{multirow}
\graphicspath{{images/}}
\usepackage[babel]{csquotes}
\usepackage[backend=biber,style=apa]{biblatex}
\DeclareLanguageMapping{spanish}{spanish-apa}
\addbibresource{Bibliography.bib}

I'm importing .tex files by \import{pathtofile}{filename} as following

\import{part_1_introduction/}{introduction.tex}
\import{part_1_introduction/}{issue_statement.tex}
\import{part_1_introduction/}{justification.tex}
\import{part_1_introduction/}{general_objective.tex}

Where each file contains following commands at the very beginning of it

\part{Parte II. Estado del arte}

or

\chapter{Marco conceptual}

3 Answers3

3

You can patch the \chapter macro to write Capítulo~ before the chapter number. Just add this to your preamble:

\usepackage{etoolbox}
\makeatletter
\patchcmd\@chapter
             {\numberline {\thechapter }}
  {\@chapapp~ \numberline {\thechapter }}
  {}{\FAIL}
\makeatother

The code above uses the \patchcmd command from etoolbox. \patchcmd takes four arguments:

\patchcmd{<command>}{<find>}{<replace>}{<success>}{<fail>}

it searches for the first occurrence of <find> the definition of <command> and, if found, replaces by <replace> and executes the <success> code. Otherwise executes the <fail> code.

I applied the patch above to the \@chapter macro which, in the book class, contains

\addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}%

which writes a line in the .toc file adding the current chapter to the table of contents.

The patch searches for \numberline{\thechapter} (which whites the chapter number) and inserts \@chapapp~ before it. \@chapapp is the word Capítulo when you are writing in spanish, but is something else in other language, and ~ is a space.

The <success> code is empty because we don't want \patchcmd to execute anything else. The fail code, on the other hand, contains \FAIL (which isn't defined) so that if, for any reason, the patch fails, \patchcmd warns you, instead of silently ignoring the failed patch.

enter image description here

Compilable example:

\documentclass[12pt, letterpaper, twoside]{book}
\usepackage[utf8]{inputenc}
\usepackage[spanish]{babel}
\usepackage[top=25mm, bottom=25mm,right=25mm,left=30mm]{geometry}
\usepackage{ragged2e}
\usepackage{graphicx}
\usepackage[table]{xcolor}
\usepackage{import}
\usepackage{enumerate}
\usepackage{tabularx,booktabs,caption}
\usepackage{multirow}
\graphicspath{{images/}}
\usepackage[babel]{csquotes}
\usepackage[backend=biber,style=apa]{biblatex}
\DeclareLanguageMapping{spanish}{spanish-apa}
% \addbibresource{Bibliography.bib}

\usepackage{etoolbox}
\makeatletter
\patchcmd\@chapter
  {\numberline {\thechapter }}
  {\@chapapp~ \numberline {\thechapter }}
  {}{\FAIL}
\makeatother

\usepackage{blindtext}% Dummy text for the example
\begin{document}
\tableofcontents
\blinddocument
\end{document}
1

Here is another suggestion using package tocbasic:

\documentclass{book}
\usepackage{blindtext}% only for dummy text

\usepackage{tocbasic}
\DeclareTOCStyleEntry[
  entrynumberformat=\entryprefix{\chaptername},% adds the prefix
  dynnumwidth% calculates the needed numberwidth
]{tocline}{chapter}

\newcommand*\entryprefix[2]{#1~#2~}
\begin{document}
\tableofcontents
\blinddocument
\blinddocument
\chapter{Chapter title with many words, so it is longer than one line and needs a second line}
\end{document}

Run three times to get

enter image description here

Note that the second line of an entry is aligned with the text of the first line.

esdd
  • 85,675
0

Using the tocloft package:

\usepackage{tocloft}
\renewcommand{\cftchappresnum}{Capitulo }

will put Capitulo and a space before the chapter number in the ToC.

Peter Wilson
  • 28,066