3

I'm new to latex.

I have successfully formatted the chapter heading, section heading and subsection heading with titlesec package:

\usepackage{titlesec}

    \titleformat {\chapter}
        [display] 
        {\normalfont\bfseries}
        {\large\thechapter}
        {0ex}
        {\LARGE \titlerule[1pt] }

    \titleformat{\section}
        {\normalfont\large\bfseries}
        {\thechapter.\thesection}
        {1ex}
        {}

    \titleformat{\subsection}
        {\normalfont\bfseries}
        {\thechapter.\thesubsection}
        {1ex}
        {}

But in the generated table of content the section and the subsection has no chapter number, while I'd like to have it.

Any hints?

EDIT: I wanted to avoid to put all the main document because it is a template for a thesis and it is full of parts that are not relevant to the problem but I'm not sure what is relevat and what is not so I will copy it all. Sorry for that.

%\documentclass[12pt,a4paper,oneside, titlepage,fleqn,italian]{book} %stampa
\documentclass[12pt,a4paper,twoside,openright,titlepage,fleqn,italian]{book}

\usepackage[italian]{babel}
\usepackage{graphicx}
\usepackage{natbib}
\usepackage[applemac]{inputenc}
\usepackage{amsmath,amssymb,amsthm,graphicx,bm, color, tikz}
\usepackage{epsfig}
\usepackage{epstopdf}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[colorlinks=false]{hyperref}
\usepackage{longtable}

\renewcommand{\thesection}{\arabic{section}}
\addtolength{\voffset}{-0,5cm}
\addtolength{\textheight}{1cm}
\setlength{\parindent}{0pt}
\frenchspacing
\linespread{1.25}

\usepackage{fancyhdr}
\usepackage{algorithm} 
\usepackage{algorithmic} 
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}%
\renewcommand{\sectionmark}[1]{\markright{\thechapter .\thesection\ #1}}%
\renewcommand{\subsectionmark}[1]{\markright{\thechapter .\thesubsection\ #1}}%
\fancyhf{}
\lhead{\rightmark}%
\chead{}%
\rhead{\bfseries\thepage}%
\renewcommand{\headrulewidth}{0.4pt}%
\renewcommand{\footrulewidth}{0pt}%
\newcommand{\tab}{\hspace*{2em}}
\DeclareMathOperator*{\argmax}{arg\,max}
\setlength{\headheight}{15pt}%
\fancypagestyle{plain}{\fancyhead{}\renewcommand{\headrulewidth}{0pt}}%


\fancyhead[RO]{\textbf{\thepage}}
\fancyhead[LO]{\nouppercase{\rightmark}}

\usepackage{frontesp} % see frontesp.sty 

\usepackage{atbeginend}

\AfterBegin{itemize}{\addtolength{\parskip}{- 9pt}}
\AfterBegin{description}{\addtolength{\itemsep}{- 9pt}}
\AfterBegin{enumerate}{\addtolength{\itemsep}{- 9pt}}

\usepackage[htt]{hyphenat}

\usepackage{titlesec}

    \titleformat {\chapter}
        [display] 
        {\normalfont\bfseries}
        {\large\thechapter}
        {0ex}
        {\LARGE \titlerule[1pt] }

    \titleformat{\section}
        {\normalfont\large\bfseries}
        {\thechapter.\thesection}
        {1ex}
        {}

    \titleformat{\subsection}
        {\normalfont\bfseries}
        {\thechapter.\thesubsection}
        {1ex}
        {}

\usepackage[font=footnotesize,labelfont=bf]{caption}

\begin{document}

\frontmatter

\title{  title  }
\author{  author  }
\date{}
\degreeyear{ 2025/26 }  

\principaladviser{ teacher }
\firstreader{ reader }

\maketitle

\textcolor{white}{.}

\addtolength{\oddsidemargin}{+1,3cm}

\addtocounter{tocdepth}{+1} % \subsubsection in the table of content


\tableofcontents

    \input{preface}

\mainmatter

    \input{cap1}
    \input{cap2}
    \input{cap3}
    \input{cap4}

\addtolength{\parskip}{5pt}


\bibliography{bibliografia}
\bibliographystyle{apalike}
% argument is your BibTeX string definitions and bibliography database(s)

\end{document}

PS: if you have any kind of advice it is welcome!

0

2 Answers 2

4

Your MWE is far from minimal...

Anyway, I bet you wanted to eliminate section numbers like 0.1 with the line

\renewcommand{\thesection}{\arabic{section}}

but this changes the behavior of the whole document.

So, the right way to do it should be:

\makeatletter
\renewcommand{\thesection}{\if@mainmatter\thechapter.\arabic{section}\else\arabic{section}\fi}
\makeatother

In this way unwanted zeroes are eliminated.

At this point you have to eliminate \thechapter when not needed from these lines

\titleformat {\chapter}
    [display] 
    {\normalfont\bfseries}
    {\large\thechapter}
    {0ex}
    {\LARGE \titlerule[1pt] }

\titleformat{\section}
    {\normalfont\large\bfseries}
    {\thechapter.\thesection}
    {1ex}
    {}

\titleformat{\subsection}
    {\normalfont\bfseries}
    {\thechapter.\thesubsection}
    {1ex}
    {}

that is to say, they have to be:

\titleformat {\chapter}
    [display] 
    {\normalfont\bfseries}
    {\large\thechapter}
    {0ex}
    {\LARGE \titlerule[1pt] }

\titleformat{\section}
    {\normalfont\large\bfseries}
    {\thesection}
    {1ex}
    {}

\titleformat{\subsection}
    {\normalfont\bfseries}
    {\thesubsection}
    {1ex}
    {}

Now the following MWE

\documentclass[12pt,a4paper,twoside,openright,titlepage,fleqn,italian]{book}

\usepackage[italian]{babel}

\makeatletter
\renewcommand{\thesection}{\if@mainmatter\thechapter.\arabic{section}\else\arabic{section}\fi}
\makeatother

\usepackage{titlesec}

    \titleformat {\chapter}
        [display]
        {\normalfont\bfseries}
        {\large\thechapter}
        {0ex}
        {\LARGE \titlerule[1pt] }

    \titleformat{\section}
        {\normalfont\large\bfseries}
        {\thesection}
        {1ex}
        {}

    \titleformat{\subsection}
        {\normalfont\bfseries}
        {\thesubsection}
        {1ex}
        {}


\begin{document}

\frontmatter

\tableofcontents

\chapter{Intro}
\section{A section}

\mainmatter

\chapter{1st}

\chapter{2nd}
\section{A section}
\subsection{A subsection}

\end{document} 

gives the desired result in the ToC

enter image description here

and in the rest of the document

enter image description here

Also remember to remove \thechapter from the lines

\renewcommand{\sectionmark}[1]{\markright{\thechapter .\thesection\ #1}}%
\renewcommand{\subsectionmark}[1]{\markright{\thechapter .\thesubsection\ #1}}%

that is, write them as

\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}%
\renewcommand{\subsectionmark}[1]{\markright{\thesubsection\ #1}}%
1
  • yes, i'm still confused on what is important for a MWE in latex, very exhaustive answer thanks
    – nkint
    Commented Jan 26, 2014 at 10:41
1

Unfortunately your code is incomplete - no \begin{document} and \end{document}, also, there's no preamble. It's usually a good idea to include these in any code you're posting here, so that responders can get it to compile straight away. Here's a sample of my code, which does what I think you're trying to do.

You should be able to just copy and past that into a new document and hit compile/process. However, you will need to process this code several times to get the table of contents to display right.

I've also put in the code for \pagenumbering{roman} which uses roman numerals for the contents page and \pagenumbering{arabic} which uses arabic numbers (e.g. 1,2,3...) for the chapters and sections. If you get rid of these lines of code, the report document style will automatically number all the pages with arabic numerals starting from the first page, in this case, the contents page.

I hope that helps.

\documentclass[12pt,a4paper]{report}

\begin{document}

\pagenumbering{roman}
\tableofcontents
\newpage

\pagenumbering{arabic}
\chapter{Your Chapter 1 Title Here}
\section{Your Section Here}
\subsection{Your Sub-section Here}

\chapter{Your Chapter 2 Title Here}
\section{Your Sub-section Here}
\subsection{Your Sub-section Here}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .