
タイトルが空のコマンドに対して異なる動作をするセクションコマンド (サブパラグラフなど) を作成したいと思います。
これは私が得たいものを少し示す私の 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}
私はこの質問の答えを見ました:タイトルが空の\section。最初の段落の横に番号を表示
希望通りの動作をしますが、 なしでも同じ結果を得ることができますかtitlesec
?
答え1
私の知る限り、これ\subparagraph{}
は LaTeX コマンドの通常の使用方法ではない\subparagraph
ので、コマンドに別の名前を付けることをお勧めします。\subparagraph
質問で尋ねられたのと同じ名前で回答します。私は 2 つの方法を提案します。どちらも を使用し、すべての可能な引数(星印の有無、角括弧内のオプション引数の有無) をxparse
簡単に取得して分析します。\subparagraph
xparse
およびetoolbox
(「LaTeX2eスタイル」)の使用
このメソッドはxparse
と を使用します。さらに、の 5 番目の引数は TeX が使用すると必ず展開されるetoolbox
という事実を利用します。 の空の必須引数を空の引数と同じように処理したい場合は、 を;に置き換えるだけで、と は同じ動作になります。\@startsection
\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
およびexpl3
(「expl3
スタイル」)の使用
以下の方法はexpl3
、LaTeX3プロジェクトの言語であるの技術を使用しています。できたしかし、 の 5 番目の引数がいずれかの時点で自動的に展開されるという事実には依存していません\@startsection
。代わりに、次の行で必要な展開を自分で実行し、その結果を に格納します\l_tmpa_tl
。
\tl_set:Nx \l_tmpa_tl { \tl_if_empty:nTF {#3} { -1em } { 1.5ex \@plus .2ex } }
次に引数型を使用しV
て\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}
出力は上記と同じです。