4

with \usepackage[explicit]{titlesec} I have defined \section like

\titleformat{\section}
  {\ttfamily\Large\bfseries\color{red}}
  {\thesection}
  {1em}
  {#1}

i want to redefine the starred version of section \section* To become like \section (with number) Except that the text after Comes in front of section heading (run in)

  • 1
    You don't need the explicit option: remove it and just leave the last argument empty. – egreg Jul 21 '14 at 8:15
4

There's a numberless key (to be used with name) that does the job:

\documentclass[12pt, a4paper]{article}%

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

\usepackage[x11names]{xcolor}
\usepackage[explicit]{titlesec}%
\usepackage{titletoc} %

\titleformat{\section}
  {\ttfamily\Large\bfseries\color{red}}
  {\thesection}
  {1em}
  {#1}%
%
\titleformat{name = \section, numberless}
  {\ttfamily\Large\bfseries\color{red}}
  {}
  {0em}
  {\addcontentsline{toc}{section}{#1}#1}%

\begin{document}

\section{A Numbered Section}
Text text text text text text text text text text text text text text text text text text text text text text text text text text text text text.

\section*{An Unnumbered Section}
Text text text text text text text text text text text text text text text text text text text text text text text text text text text text text.

\section{Still Another Numbered Section}
Text text text text text text text text text text text text text text text text text text text text text text text text text text text text text.

\end{document} 

enter image description here

| improve this answer | |
  • \section* with section number and run in mode – Salim Bou Jul 21 '14 at 8:38
  • I misunderstood. OK, I'll modify at once. – Bernard Jul 21 '14 at 8:41
  • @seloumi: I updated my answer, but there will be problems if you want a table of contents. – Bernard Jul 21 '14 at 8:59
  • by unnumbered you mean numberless, right? :) – cgnieder Jul 21 '14 at 11:04
  • Yes. I wanted to make a difference between the meaning and the key that's used. – Bernard Jul 21 '14 at 11:21
2

If you do not need a table of contents you can delete the lines for the toc redefinition. However a solution with a special macro like \Ssection} maybe a better choice, then \ţableofcontents hasn't to be redefined:

\documentclass[12pt, a4paper]{article}%
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{xcolor}
\usepackage[explicit]{titlesec}%
\let\TableOfContents\tableofcontents
\renewcommand\tableofcontents{{\titleformat{\section}%
   {\Large\bfseries}{\thesection}{1em}{##1}\let\section\Section\TableOfContents}}
\let\Section\section
\makeatletter
\renewcommand\section{\@ifstar\starsection\Section}
\makeatother
\newcommand\starsection[1]{%
  \par\bigskip\noindent\refstepcounter{section}%
  \addcontentsline{toc}{section}{\thesection\hspace{0.8em}#1}%
  {\ttfamily\Large\bfseries\textcolor{red}{\thesection\hspace{1em}#1}\hspace{1em}}}

\titleformat{\section}
  {\ttfamily\Large\bfseries\color{red}}
  {\thesection}
  {1em}
  {#1}

\begin{document}
\tableofcontents
\section{A Numbered Section}
Text text text text text text text text text text text text text text text text text text text text text text text text text text text text text.

\section*{An Unnumbered Section}
Text text text text text text text text text text text text text text text text text text text text text text text text text text text text text.

\end{document} 

enter image description here

| improve this answer | |
  • Is there a solution if one wants \contentsname be typeset ‘ normally’? – Bernard Jul 21 '14 at 12:02
  • sure. Redefine \titleformat – user2478 Jul 21 '14 at 12:17

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.