擴張?骯髒的伎倆?

擴張?骯髒的伎倆?

考慮

\documentclass{article}
\usepackage{titlesec}

\def\bl#1 #2 {#1\textsuperscript{#2}\,}
\titleformat{\section}[runin]{}{}{0pt}{}[. ]
%\titleformat{\section}[runin]{}{}{0pt}{\bl}[. ]

\begin{document}
\section{3 a 21}
main text.
\end{document}

註解掉該行的目的是產生等效的 ,而\section{\bl 3 a 21}不必為每個部分鍵入 \bl ;但它產生了一個錯誤。可以做些什麼來達到預期的結果嗎?

答案1

您必須在將參數傳遞給 之前擷取該參數\bl。下面我們\@bl在將其傳遞給之前使用捕獲它\bl

在此輸入影像描述

\documentclass{article}
\usepackage{titlesec}

\makeatletter
\def\@bl#1{\bl#1}
\def\bl#1 #2 {#1\textsuperscript{#2}\,}
\titleformat{\section}[runin]{}{}{0pt}{\@bl}[. ]
\makeatother

\begin{document}
\section{3 a 21}
main text.
\end{document}

答案2

您也可以在沒有任何軟體包的情況下完成此操作。技巧在於, 的參數會\section作為大括號組傳遞到程式碼的最後部分,因此如果該部分以帶有參數的巨集結尾,它將看到節標題;但你首先需要取下牙套。請注意,使用三個參數定義內部宏,我們可以輕鬆新增最後一個句點。

\documentclass{article}
\usepackage{showframe}% just for the example

\makeatletter
\renewcommand{\section}{%
  \@startsection{section}{1}{\z@}%
    {-3.5ex \@plus -1ex \@minus -.2ex}%
    {-1em}%
    {\normalfont\process@section@title}%
}
\newcommand{\process@section@title}[1]{\process@section@title@aux#1\@nil}
\def\process@section@title@aux#1 #2 #3\@nil{%
  #1\textsuperscript{#2} #3.%
}
\makeatother

\setcounter{secnumdepth}{0}

\begin{document}

\section{3 a 21}
Some text for the section.

\section{4 a 42}
Some text for the section.

\end{document}

在此輸入影像描述

答案3

這可能符合「髒」的條件:

\documentclass{article}
\usepackage{titlesec}

\def\bl#1 #2 {#1\textsuperscript{#2}\,}
\titleformat{\section}[runin]{}{}{0pt}{}[. ]
\let\oldsect=\section
\def\section#1{\oldsect{\bl#1}}
\begin{document}
\section{3 a 21}
main text.
\end{document}

3a 21. 正文。

相關內容