Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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)

share|improve this question
    
You don't need the explicit option: remove it and just leave the last argument empty. –  egreg Jul 21 at 8:15

2 Answers 2

up vote 3 down vote accepted

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}%

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

\titleformat{name = \section, numberless}[runin]
  {\ttfamily\Large\bfseries\color{red}}
  {\stepcounter{section}\thesection}
  {1em}
  {#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{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

However, be aware that it will be more complex if you also want a table of contents as it would then be numbered and in run-in style.

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

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

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.