タイトルが指定されていない場合はサブセクションの形式が異なります

タイトルが指定されていない場合はサブセクションの形式が異なります

\subsection{Foo}と入力するとサブセクションが「Article 1: Foo」のように表示され、 (タイトルなし) と入力すると「Article 1」(コロンなし) のように表示されるようにしたいです\subsection{}。 これを実現する方法はありますか?

現在私が持っている MWE は次のとおりです。

\documentclass{article}

\usepackage{titlesec}

\newcounter{artikel}
\setcounter{artikel}{1}

\renewcommand{\thesubsection}{\arabic{artikel}}
\titleformat{\subsection}{\large\bfseries}{}{0pt}{Article \thesubsection: \refstepcounter{artikel}}

\begin{document}

\subsection{Foo}
\subsection{}

\end{document}

それによって

第1条:フー

第2条:

しかし、私は次のような成果をあげたいのです:

第1条:フー

第2条

答え1

これはetoolbox、 とexplicitのオプションを使用して実行できますtitlesec。また、サブセクションを使用する場合はstarred、 キーを使用して異なる書式設定を行うことができますnumberless

\documentclass{article}

\usepackage[explicit]{titlesec}
\usepackage{etoolbox}
\newcounter{artikel}
\setcounter{artikel}{1}

\renewcommand{\thesubsection}{\arabic{artikel}}
\titleformat{\subsection}{\large\bfseries}{}{0pt}{Article \thesubsection\notblank{#1}{:~}{}#1\refstepcounter{artikel}}

\begin{document}

    \subsection{Foo}
    \subsection{ }

\end{document} 

ここに画像の説明を入力してください

関連情報