在 (纯) (La)TeX 中有没有办法确定某个小节是否直接跟在某个节之后?
例如
\section{Main level}
\subsection{Sublevel}
我想在小节下画一条线,
\section{Main level}
some text
\subsection{Sublevel}
我想在该部分下画一条线(不是在文本下划线,而是在标题和文本之间画一条简单的 hsize 宽度规则)。
再补充一点:我不能包含任何外部包,因为它们可能会干扰我现有的布局,因此我需要一个相当低级的解决方案来解决这个问题。这可能吗?
感谢 Ulrike Fischer 的回答,我最终得到了类似的东西
\documentclass{article}
\makeatletter
\newif\if@dolinesec\@dolinesecfalse
\def\section{\@dolinesectrue\@startsection{section}{1}{\z@}
{-2em\@plus-1ex\@minus-.2ex}
{1em\@plus.2ex}
{\normalsize\bfseries}}
\def\subsection{\@startsection{subsection}{2}{\z@}
{-1em\@plus-0.25ex\@minus-.2ex}
{1em\@plus.2ex}
{\normalsize\itshape\bfseries}}
\def\subsubsection{\@startsection{subsubsection}{3}{\z@}
{-3.25ex\@plus-1ex\@minus-0.2ex}
{1.5ex\@plus0.2ex}
{\normalfont\sffamily}}
\def\@afterheading{%
\@nobreaktrue
\everypar{%
\if@nobreak
\@nobreakfalse
\clubpenalty \@M
\if@afterindent \else
{\setbox\z@\lastbox
\if@dolinesec\rlap{\raise\baselineskip\hbox{\smash{\vrule width\hsize height.4\p@\relax}}}\fi%
}%
\fi
\@dolinesecfalse
\else
\clubpenalty \@clubpenalty
\everypar{}%
\fi}}
\makeatother
\begin{document}
\section{Test}
\subsection{Sub Test}
text text text text text text text text text text text text
text text text text text text text text text text text text
\subsubsection{Sub Test}\label{blabla}
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{Test}
text text text text text text text text text text text text
text text text text text text text text text text text text
\subsection{Sub Test}
text text text text text text text text text text text text
text text text text text text text text text text text text
\subsubsection{Sub Test}\label{blabla}
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{Test}\label{bla}
\subsection{Sub Test}\label{blabla}
\subsubsection{Sub Test}\label{blabla}
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 text text text text text text text
text text text text text text text text text text text text
\end{document}
这给了我:
答案1
添加规则\@afterheading
实际上已经或多或少给出了想要的结果。它需要一些调整(例如,避免它也影响 \subsubsections 等),但我现在没有时间:
\documentclass{article}
\usepackage[utf8]{inputenc}
\makeatletter
\def\@afterheading{%
\@nobreaktrue
\everypar{%
\if@nobreak
\@nobreakfalse
\clubpenalty \@M
\if@afterindent \else
{\setbox\z@\lastbox
\hrule\vspace{1ex}%<---
}%
\fi
\else
\clubpenalty \@clubpenalty
\everypar{}%
\fi}}
\makeatother
\begin{document}
\section{Test}
\subsection{Sub Test}
text
\subsection{Sub Test}
text
\section{Test}
\subsection{Sub Test}
text
\section{Test}
text
\subsection{Sub Test}
\section[optional]{Test}
\subsection[optional]{Sub Test}
text
\section{blblblb}
vlvlv
\end{document}
答案2
正如一些人指出的那样,仅当和之间没有多余的标记(例如,\label
甚至是空行)时,这种方法才有效。\section
\subsection
\documentclass{article}
\usepackage{lipsum}
\let\svsection\section
\let\svsubsection\subsection
\renewcommand\section[3][]{%
\ifx\relax#1\relax\svsection{#2}\else\svsection[#1]{#2}\fi%
\ifx\subsection#3%
\def\next{\xsubsection}%
\else%
\noindent\rule[\ht\strutbox]{\linewidth}{.4pt}\par%
\def\next{#3}%
\fi%
\next
}
\newcommand\xsubsection[2][]{%
\ifx\relax#1\relax\svsubsection{#2}\else\svsubsection[#1]{#2}\fi%
\noindent\rule[.5\ht\strutbox]{\linewidth}{.4pt}\par%
}
\begin{document}
\section{Test}
\subsection{Sub Test}
\lipsum[4]
\section{Test}
\lipsum[4]
\subsection{Sub Test}
\section[optional]{Test}
\subsection[optional]{Sub Test}
\lipsum[4]
\section[optional]{Test}
\lipsum[4]
\subsection[optional]{Sub Test}
\end{document}