0

How can I replace the space after the subsection title with a full stop?

Screenshot

MWE:

\documentclass[a4paper,twoside,11pt,openright]{book}
\usepackage{fancyhdr}
\usepackage{imakeidx}
\usepackage{etoolbox}

   \makeindex[intoc]
   \pagestyle{fancy}
   
\usepackage{sectsty}
\usepackage{lipsum}


\makeatletter

\let\@ssect@ORIG\@ssect
\let\@runin@ssect\@ssect

\apptocmd{\@runin@ssect}{%
 \addcontentsline{toc}{subsection}{%
   % Comment out the following line to remove “phantom number” indentation in
   % the TOC
   \protect\numberline{}% no number in TOC
   #5% the title
 }%
 \let\@ssect\@ssect@ORIG       % restore the normal \@ssect
}{}{\FAILED}

\newcommand*{\runinsubsection}{%
 \let\@ssect\@runin@ssect
 \@startsection{subsection}%
 {2}% level
 {\z@}% indentation of heading from the left margin
 {-3.25ex\@plus -1ex \@minus -.2ex}% absolute value = beforeskip
 {-1.5em \@plus -.1em}% when negative, opposite = skip to leave right of a
                      % run-in heading.
 {\normalfont\large\bfseries}% style
 *% we want an unnumbered subsection
}
\makeatother



\begin{document}
   \runinsubsection{First}
       \lipsum[1]
\end{document}
7
  • You load titlesec: why don't you use it?
    – egreg
    Mar 22, 2022 at 8:26
  • It doesn't work with sectsy. Mar 22, 2022 at 8:47
  • You never want to load both titlesec and sectsty
    – egreg
    Mar 22, 2022 at 9:02
  • @egreg Right. I'll remove titlesec. Mar 22, 2022 at 9:11
  • unrelated but book class has not got an option twoside=true perhaps you intended twoside Mar 22, 2022 at 9:25

2 Answers 2

1

This is much simpler than your attempt.

\documentclass[a4paper,twoside,11pt,openright]{book}

\usepackage{lipsum}

\makeatletter
\newcommand*{\runinsubsection}[1]{%
  \@startsection{subsection}%
  {2}% level
  {\z@}% indentation of heading from the left margin
  {-3.25ex\@plus -1ex \@minus -.2ex}% absolute value = beforeskip
  {-1.5em \@plus -.1em}% when negative, opposite = skip to leave right of a
                       % run-in heading.
  {\normalfont\large\bfseries}% style
  *%
  {#1.}%
  \addcontentsline{toc}{subsection}{\protect\numberline{}#1}%
}
\makeatother



\begin{document}

\tableofcontents

\section{x}
\subsection{y}

\lipsum[1][1-4]

\runinsubsection{First}
\lipsum[1]

\end{document}

enter image description here

If you want a normal space after the period, use

\makeatletter
\newcommand*{\runinsubsection}[1]{%
  \@startsection{subsection}%
  {2}% level
  {\z@}% indentation of heading from the left margin
  {-3.25ex\@plus -1ex \@minus -.2ex}% absolute value = beforeskip
  {0pt}% when negative, opposite = skip to leave right of a
                       % run-in heading.
  {\normalfont\large\bfseries}% style
  *%
  {#1. \hspace{0pt}}%
  \addcontentsline{toc}{subsection}{\protect\numberline{}#1}%
}
\makeatother

enter image description here

2
  • Thank you. Any way to remove the extra space after the full stop? Mar 22, 2022 at 12:03
  • 1
    @SahibaArora Added the trick.
    – egreg
    Mar 22, 2022 at 16:05
-1

Why not use titlesec for that? It is quite easy:

\documentclass[a4paper,twoside=true,11pt,openright]{book}
\usepackage{fancyhdr}
\usepackage{imakeidx}
\usepackage{etoolbox}
\usepackage{fourier}
   \makeindex[intoc]
   \pagestyle{fancy}

\usepackage{titlesec}
\usepackage{lipsum}

\titleformat{\subsection, }[runin]{\large\bfseries}{}{}{}[.]

\begin{document}

\subsection*{First} In former days -- that is to say, once upon a time, there lived in the Land of Gramblamble, Seven Families. They lived by the side of the great Lake Pipple-popple (one of the Seven Families, indeed, lived in the Lake), and on the outskirts of the City of Tosh, which, excepting when it was quite dark, they could see plainly. The names of all these places you have probably heard of, and you have only not to look in your Geography books to find out all about them.

\end{document} 

enter image description here

2
  • Because I don't want to get rid of sectsty. Mar 22, 2022 at 10:44
  • However, titlesec is more powerful
    – Bernard
    Mar 22, 2022 at 11:11

You must log in to answer this question.

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