1

I have searched a lot to find an answer to this question, but I could only find how to remove a newline after \section (or \chapter).

How can one remove the newline before \section, as defined by book class?

Basically, looking at the MWE below, I am looking to a way to have:

Text before. Section heading

MWE:

\documentclass[12pt]{book}        
\usepackage{titlesec}    
\titleformat{\section}[hang]{\normalfont}{\thesection.}{0pt}{}

\titlespacing{\section}
   {0pt}% left
   {0pt}% before
   {0pt}% after

\begin{document}
Text before. \section*{Section heading} Text after
\end{document}

I can't find any option with titlesec and I would not want to enter the esoteric intricacies of TeX class redefinitions.

I would be grateful for any hint or pointer on how to deal with this kind of problem.

gsl
  • 699

1 Answers1

3

enter image description here

\documentclass[12pt]{book}        
\makeatletter
\renewcommand\section{\@ifstar\mystarsec\mynostarsec}
\makeatother

\newcommand\mystarsec[1]{%
\ifhmode\unskip\fi\quad\textbf{#1}}
\newcommand\mynostarsec[1]{%
\ifhmode\unskip\fi\refsetepcounter{section}\quad\textbf{\thesection. #1}}

\begin{document}
Text before. \section*{Section heading} Text after
\end{document}
David Carlisle
  • 757,742
  • Nice! Would this preserve all the benefits given by the standard book class for that command (like TOC, etc)? – gsl Oct 27 '14 at 15:57
  • 1
    @gsl no, as written it doesn't have the [short for toc] option nor does it write to the TOC (or the page head) it could (I may update later if you want that) but in that case it would be a (bit) easier if instead of redefining \section you redefined the lower level \@startsection but in that case \subsection, \paragraph etc would also be redefined to be inline, would that be OK? – David Carlisle Oct 27 '14 at 16:02
  • Yes, that would be better (to redefine \@startsection), as it seems it would be easier later on to add space where necessary than to remove. Thank you so much. – gsl Oct 27 '14 at 16:05