2

I have problems when trying to compile a document with \section modified. I wrote the following code for my format:

\renewcommand{\thesection}{\Roman{section}} 
\titleformat{\section}{\centering \bf \Large}{Título}{0.3em}{\thesection\\}
\renewcommand{\thesubsection}{\arabic{subsection}}

But, after the titlepage, I insert a \tableofcontents which is throwing the error: LaTeX Error: There's no line here to end.

I tried replacing the \\ from the \titleformat with \newline but the result is not what I need (that does not throw the error).

The question: Is there any simple way I can get this output when writing \section{I'm a Title!}?

Needed output

EDIT: Sample Code:

\documentclass{article}
\usepackage[spanish]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{titlesec}

\renewcommand{\thesection}{\Roman{section}} 
\titleformat{\section}{\centering \bf \Large}{Título}{0.3em}{\thesection\\}
\renewcommand{\thesubsection}{\arabic{subsection}}

\begin{document}

\tableofcontents

\section{I'm a Title!}

\end{document}
Mico
  • 506,678
M Aubel
  • 23
  • Thanks for the advice, I just added a short code to see the output. I'm using \documentclass{article}btw – M Aubel Dec 16 '18 at 02:55
  • Are you aware of the fact that your code doesn't work correctly with unnumbered sectioning headers, i.e., those produced by \section*? – Mico Dec 16 '18 at 07:27
  • @Mico yes I'm aware, but all my sections will be numbered. Thanks! – M Aubel Dec 16 '18 at 20:20
  • @MAubel - I guess the good news is that the code in my answer works with both numbered and unnumbered sections. :-) – Mico Dec 16 '18 at 20:22

1 Answers1

1

(revised my answer after coming across the posting Line break after \section number)

The following code is based on this answer to the query Line break after \section number. The solution lies in changing the arguments of \titleformat. Specifically, instead of

\titleformat{\section}{\centering \bf \Large}{Título}{0.3em}{\thesection\\}

you should write

\titleformat{\section}[display]{\normalfont\Large\bfseries\filcenter}{Título \thesection}{0.3em}{}

enter image description here

\documentclass{article}
\usepackage[spanish]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{titlesec}
\titleformat{\section}[display]%
    {\normalfont\Large\bfseries\filcenter}{Título \thesection}{0.3em}{}

\renewcommand{\thesection}{\Roman{section}} 
\renewcommand{\thesubsection}{\arabic{subsection}}

\begin{document}
\tableofcontents
\section{I'm a Title!}
\subsection{And I'm a Subtitle!}
\end{document}
Mico
  • 506,678