将小节标题后的空格替换为句号

将小节标题后的空格替换为句号

我怎样才能用句号替换小节标题后的空格?

截屏

梅威瑟:

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

答案1

这比你的尝试简单得多。

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

在此处输入图片描述

如果希望句号后有正常空格,请使用

\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

在此处输入图片描述

答案2

为什么不使用titlesec它呢?这很容易:

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

在此处输入图片描述

相关内容