
我想製作一個分段命令(例如分段),該命令對於具有空標題的命令將具有不同的行為。
這是我的 MWE,展示了我想要得到的一些資訊:
\documentclass{article}
\setcounter{secnumdepth}{7}
\makeatletter
\renewcommand\subparagraph{\@startsection{subparagraph}{5}{\parindent}%
{3.25ex \@plus1ex \@minus .2ex}%
{1.5ex \@plus .2ex}%
{\normalfont\normalsize\bfseries}}
\makeatother
\begin{document}
\subparagraph{Subparagraph with title}
Some random text
\makeatletter
\renewcommand\subparagraph{\@startsection{subparagraph}{5}{\parindent}%
{3.25ex \@plus1ex \@minus .2ex}%
{-1em}%
{\normalfont\normalsize\bfseries}}
\makeatother
\subparagraph{} Some random text for subparagraph without title
\end{document}
我看了這個問題的答案:\ 標題為空的部分。在第一段旁邊顯示編號
它做了與我想要的相同的事情,但是如果沒有 ,我可以獲得相同的結果嗎titlesec
?
答案1
據我所知,\subparagraph{}
這不是使用 LaTeX 命令的正常方式\subparagraph
,所以給你的命令一個不同的名稱可能是個好主意。無論如何,我都會用名字來回答\subparagraph
,因為這是問題中所問的。我提出了兩種方法,這兩種方法都是xparse
為了輕鬆取得和分析所有可能的參數\subparagraph
(有或沒有星號,有或沒有方括號內的可選參數)。
使用xparse
and etoolbox
(“LaTeX2e 風格”)
該方法使用xparse
和etoolbox
。此外,它還利用了這樣一個事實:\@startsection
當 TeX 使用它時,第五個參數必然會被擴展。如果您希望\subparagraph
以與空參數相同的方式處理您的空白強制參數,只需替換\ifstrempty
為\ifblank
;然後,\subparagraph{ }
並將\subparagraph{}
有相同的行為。
\documentclass{article}
\usepackage{xparse}
\usepackage{etoolbox}
\usepackage{lipsum}
\setcounter{secnumdepth}{7}
\makeatletter
\newcommand*{\alecheim@subparagraph}[1]{%
\@startsection{subparagraph}{5}{\parindent}%
{3.25ex \@plus1ex \@minus .2ex}%
% The following argument will be expanded during <glue> assignments
{\ifstrempty{#1}{-1em}{1.5ex \@plus .2ex}}
{\normalfont\normalsize\bfseries}%
}
\RenewDocumentCommand{\subparagraph}{sO{#3}m}{%
\IfBooleanTF{#1}
{\alecheim@subparagraph{#3}*{#3}}
{\alecheim@subparagraph{#3}[#2]{#3}}%
}
\makeatother
\begin{document}
\subparagraph{Subparagraph with title}
\lipsum[1][1-2]
\subparagraph{} Some random text for subparagraph without title.
\end{document}
使用xparse
and expl3
(“expl3
樣式”)
以下方法使用來自expl3
LaTeX3 專案的語言 的技術。它可以\@startsection
但不依賴第五個參數將在某個時刻自動擴展的事實;相反,我們在以下行中自行進行所需的擴展,並將結果儲存在\l_tmpa_tl
:
\tl_set:Nx \l_tmpa_tl { \tl_if_empty:nTF {#3} { -1em } { 1.5ex \@plus .2ex } }
然後我們使用V
參數類型 with\alecheim_subparagraph:V \l_tmpa_tl
來\alecheim_subparagraph:n
傳遞價值的\l_tmpa_tl
。這是完整的範例:
\documentclass{article}
\usepackage{xparse}
\usepackage{lipsum}
\setcounter{secnumdepth}{7}
\makeatletter
\ExplSyntaxOn
\cs_new_protected:Npn \alecheim_subparagraph:n #1
{
\@startsection { subparagraph } { 5 } { \parindent }
{ 3.25ex \@plus 1ex \@minus .2ex }
{#1}
{ \normalfont \normalsize \bfseries }
}
\cs_generate_variant:Nn \alecheim_subparagraph:n { V }
\RenewDocumentCommand { \subparagraph } { s O{#3} m }
{
% You may want to replace \tl_if_empty:nTF with \tl_if_blank:nTF here.
\tl_set:Nx \l_tmpa_tl
{ \tl_if_empty:nTF {#3} { -1em } { 1.5ex \@plus .2ex } }
\IfBooleanTF {#1}
{ \alecheim_subparagraph:V \l_tmpa_tl * {#3} }
{ \alecheim_subparagraph:V \l_tmpa_tl [#2]{#3} }
}
\ExplSyntaxOff
\makeatother
\begin{document}
\subparagraph{Subparagraph with title}
\lipsum[1][1-2]
\subparagraph{} Some random text for subparagraph without title.
\end{document}
輸出與上面相同。