4

I am trying to write a Preface with subsections, but without sectionnumbers (since the preface is technically not really part of the report). The sections and subsections are constructed using \titleformat like so:

\titleformat{\section}
{\normalfont\Large\bfseries\filcenter}
{\thesection.}{0.5em}{#1}
[\vspace{3pt}
{\color{\colorstyle}{\titlerule[1.5pt]}}
\vspace{1pt}
\titlerule]
\titlespacing*{\section}
{0em}{2em}{1em}

\titleformat{\subsection}
{\normalfont\bfseries\filcenter}
{}{0pt}
{\textcolor{\colorstyle}{\noindent\Vhrulefill}\hspace{1em}
\thesubsection.
\hspace{0.5em}#1\hspace{1em}\textcolor{\colorstyle}{\Vhrulefill}}
\titlespacing*{\subsection}
{0em}{2em}{0.5em}

However, \section* works fine and there is no number, but \subsection* are numbered as "0.0 subsectiontitle", but I don't want any numbering. Does anyone know why this happens and how to avoid it?

Working example:

\documentclass{article}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Style: Fonts
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{mathptmx}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Style: Colors
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage[usenames,dvipsnames]{xcolor}

\newcommand{\colorstyle}[1]{\def\colorstyle{#1}}
\definecolor{mycolor}{RGB}{0,80,158}
\colorstyle{mycolor}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Style: Sections
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage[explicit,pagestyles]{titlesec}

\def\Vhrulefill{\leavevmode\leaders\hrule height 0.7ex depth \dimexpr0.4pt
0.7ex\hfill\kern0pt}

\titleformat{\section}
{\normalfont\Large\bfseries\filcenter}
{\thesection.}{0.5em}{#1}
[\vspace{3pt}
{\color{\colorstyle}{\titlerule[1.5pt]}}
\vspace{1pt}
\titlerule]
\titlespacing*{\section}
{0em}{2em}{1em}

\titleformat{\subsection}
{\normalfont\bfseries\filcenter}
{}{0pt}
{\textcolor{\colorstyle}{\noindent\Vhrulefill}\hspace{1em}
\thesubsection.
\hspace{0.5em}#1\hspace{1em}\textcolor{\colorstyle}{\Vhrulefill}}
\titlespacing*{\subsection}
{0em}{2em}{0.5em}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\section*{Section without a number}
Bacon ipsum dolor sit amet pork loin leberkas turducken.

\subsection*{Subsection behaving oddly}
Beef pork belly porchetta turducken, bresaola venison boudin doner.

\end{document}
4
  • 1
    Welcome to TeX.SX! You have the \thesubsection command in the fifth argument to \titleformat instead of in the third.
    – egreg
    Commented Apr 3, 2014 at 11:59
  • Hi, thank you, but I want the blue lines on both sides of the text '\textcolor{\colorstyle}{\noindent\Vhrulefill}', where '\colorstyle' is blue. If I move it from the 5th to the 3rd, the blue lines on both sides disappear :/
    – Gudrun
    Commented Apr 3, 2014 at 12:11
  • Please, make a minimal working example (MWE); I get \colorstyle and \Vhrulefill undefined.
    – egreg
    Commented Apr 3, 2014 at 12:14
  • Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}.
    – user31729
    Commented Apr 3, 2014 at 12:15

1 Answer 1

3

Use the numberless feature:

\documentclass{article}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Style: Fonts
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{mathptmx}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Style: Colors
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage[usenames,dvipsnames]{xcolor}

\newcommand{\colorstyle}[1]{\def\colorstyle{#1}}
\definecolor{mycolor}{RGB}{0,80,158}
\colorstyle{mycolor}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Style: Sections
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage[explicit,pagestyles]{titlesec}

\def\Vhrulefill{\leavevmode\leaders\hrule height 0.7ex depth \dimexpr0.4pt-0.7ex\hfill\kern0pt}

\titleformat{\section}
  {\normalfont\Large\bfseries\filcenter}
  {\thesection.}
  {0.5em}
  {#1}
  [\vspace{3pt}%
   {\color{\colorstyle}{\titlerule[1.5pt]}}
   \vspace{1pt}
   \titlerule]

\titlespacing*{\section}
  {0em}
  {2em}
  {1em}

\titleformat{\subsection}
  {\normalfont\bfseries\filcenter}
  {}
  {0pt}
  {\textcolor{\colorstyle}{\noindent\Vhrulefill}%
   \hspace{1em}\thesubsection.\hspace{0.5em}#1\hspace{1em}%
   \textcolor{\colorstyle}{\Vhrulefill}}

\titleformat{name=\subsection,numberless}
  {\normalfont\bfseries\filcenter}
  {}
  {0pt}
  {\textcolor{\colorstyle}{\noindent\Vhrulefill}%
   \hspace{1em}#1\hspace{1em}%
   \textcolor{\colorstyle}{\Vhrulefill}}

\titlespacing*{\subsection}
  {0em}
  {2em}
  {0.5em}

\begin{document}

\section{Abc def ghi}

\subsection{Abc def ghi}

\section*{Abc def ghi}

\subsection*{Abc def ghi}

\end{document}

enter image description here

0

You must log in to answer this question.

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