同一行中的连续段落标题

同一行中的连续段落标题

如何让 LaTeX 在同一行中写出段落级别的连续标题?

在以下最小示例中,我添加了原来的\paragraph\@startsection和的定义\@xsect

\documentclass{book}

\makeatletter
%% from latex.ltx, unchanged
\def\@startsection#1#2#3#4#5#6{%
  \if@noskipsec \leavevmode \fi
  \par
  \@tempskipa #4\relax
  \@afterindenttrue
  \ifdim \@tempskipa <\z@
    \@tempskipa -\@tempskipa \@afterindentfalse
  \fi
  \if@nobreak
    \everypar{}%
  \else
    \addpenalty\@secpenalty\addvspace\@tempskipa
  \fi
  \@ifstar
    {\@ssect{#3}{#4}{#5}{#6}}%
    {\@dblarg{\@sect{#1}{#2}{#3}{#4}{#5}{#6}}}}

\def\@xsect#1{%
  \@tempskipa #1\relax
  \ifdim \@tempskipa>\z@
    \par \nobreak
    \vskip \@tempskipa
    \@afterheading
  \else
    \@nobreakfalse
    \global\@noskipsectrue
    \everypar{%
      \if@noskipsec
        \global\@noskipsecfalse
       {\setbox\z@\lastbox}%
        \clubpenalty\@M
        \begingroup \@svsechd \endgroup
        \unskip
        \@tempskipa #1\relax
        \hskip -\@tempskipa
      \else
        \clubpenalty \@clubpenalty
        \everypar{}%
      \fi}%
  \fi
  \ignorespaces}

%% from book.cls
\renewcommand\paragraph{\@startsection{paragraph}{4}{\z@}%
                                    {3.25ex \@plus1ex \@minus.2ex}%
                                    {-1em}%
                                    {\normalfont\normalsize\bfseries}}
\makeatother

\begin{document}

Text
\paragraph{Test 1} Text

\paragraph{Test 2}
\paragraph{Test 3} Text

\paragraph{Test 4} Text
\end{document}

我想要的输出是这样的

期望输出


几小时后更新

我进行了一些实验并得出了以下解决方案:

\documentclass{book}

\makeatletter
%% \@tempboxa apparently is used by the counter of \section, so we create our own box:
\newbox \@tempsecbox
\def\@startsection#1#2#3#4#5#6{%
  % prevent preceeding heading from being expanded too early:
  % \if@noskipsec \leavevmode \fi
  \par
  \@tempskipa #4\relax
  \@afterindenttrue
  \ifdim \@tempskipa <\z@
    \@tempskipa -\@tempskipa \@afterindentfalse
  \fi
  \if@nobreak
    \everypar{}%
  \else
    \addpenalty\@secpenalty\addvspace\@tempskipa
  \fi
  \@ifstar
    {\@ssect{#3}{#4}{#5}{#6}}%
    {\@dblarg{\@sect{#1}{#2}{#3}{#4}{#5}{#6}}}}
\def\@xsect#1{%
  \@tempskipa #1\relax
  \ifdim \@tempskipa>\z@
    \par \nobreak
    \vskip \@tempskipa
    \@afterheading
  \else
    \@nobreakfalse
    \global\@noskipsectrue
    %% without the \leavevmode in \@startsection the preceding heading
    %% is never printed, so we store it in a temporary box which
    %% "grows" with each new consecutive heading
    \global\setbox\@tempsecbox\hbox{\ifvoid\@tempsecbox\else\unhbox\@tempsecbox\space\fi\@svsechd}%
    \everypar{%
      \if@noskipsec
        \global\@noskipsecfalse
       {\setbox\z@\lastbox}%
        \clubpenalty\@M
        %% print the fully grown heading:
        \begingroup \unhbox\@tempsecbox \endgroup
        \unskip
        \@tempskipa #1\relax
        \hskip -\@tempskipa
      \else
        \clubpenalty \@clubpenalty
        \everypar{}%
        %% Reset the \@tempsecbox
        \global\setbox\@tempsecbox\box\voidb@x
      \fi}%
  \fi
  \ignorespaces}

\renewcommand\paragraph{\@startsection{paragraph}{4}{\z@}%
                                    {3.25ex \@plus1ex \@minus.2ex}%
                                    {-1em}%
                                    {\normalfont\normalsize\bfseries}}

\makeatother

\begin{document}

Text
\paragraph{Test 1} Text

\paragraph{Test 2}
\paragraph{Test 3} Text

\paragraph{Test 4} Text

\end{document}

但是,我不太确定这个解决方案是否没有不可预见的后果。我也尝试过混合不同级别的内联和独立标题,似乎有效。

相关内容