「section」を「\section」に変換してtitleformatコマンドに渡します

「section」を「\section」に変換してtitleformatコマンドに渡します

\titleformatパッケージのコマンドはtitlesec次のように呼び出されます。

\titleformat{\section}{<a few formatting options>}
\titleformat{\subsection}{<a few formatting options>}

私は、セクションタイトルの書式設定を含むいくつかの書式設定を自動化する小さなパッケージを書いています。これは、titlesec私が「ラップしている」パッケージの1つです。問題は、コードを次の形式で記述したいということです。

\sectionfamily{section}{uppercase}
\sectionfamily{subsection}{italic}

以下は、\sectionfamily実行の一部として最終的に を呼び出すカスタム コマンドです\titleformat。質問のために、次のように定義されていると仮定します。

\newcommand{\sectionfamily}[2]{   
    \titleformat{\#1}{\itshape}{\thesection}{0pt}{}[]
}

ご覧のとおり、\sectionfamily{section}{...}を に変換しようとしています\titleformat{\section}{...}が、ここで定義した方法は機能しません。 、 などをさまざまな組み合わせで試しましたが\expandafter\csnameそれらの組み合わせのいずれも機能しませんでした。エイリアス コマンドを作成してエイリアスを渡すことも失敗しました。

sectionを変換して\section動作させる方法をご存知の方はいらっしゃいますか?


注: コマンドは\titleformat{\section}{\itshape}{\thesection}{0pt}{}[]動作し、テストに使用できます。完全なMWE(または動作しない最小限の例!!)は

\documentclass[11pt,oneside,a4paper]{article}
\usepackage{titlesec} 

\begin{document}

\section{How does this look?}

\newcommand{\sectionfamily}[2]{   
   \titleformat{\#1}{\itshape}{\thesection}{0pt}{}[]
}
\sectionfamily{section}{italic}
%What I want to run:
%\titleformat{\section}{\itshape}{\thesection}{0pt}{}[]
\section{How does this look?}
\end{document}

答え1

2 つのステートメントが必要です\expandafter。最初のステートメントはコマンド シーケンスを生成するためのもので、次に\titleformat内部セットアップで展開するためのものです (ただし、パッケージについては調べていません)。

\documentclass[11pt,oneside,a4paper]{article}
\usepackage{titlesec} 


\newcommand{\sectionfamily}[2]{%   
  \expandafter\titleformat\expandafter{\csname #1\endcsname}{\itshape}{\csname the#1\endcsname~}{0pt}{}[]
}


\sectionfamily{section}{italic}


\sectionfamily{subsection}{italic}


\begin{document}

\section{How does this look?}

%\newcommand{\sectionfamily}[2]{   
%   \titleformat{\#1}{\itshape}{\thesection}{0pt}{}[]
%}

%What I want to run:
%\titleformat{\section}{\itshape}{\thesection~}{0pt}{}[]
\section{How does this look?}

\subsection{Happy?}
\end{document}

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

答え2

使用expl3(によってロードxparse):

\documentclass[11pt,oneside,a4paper]{article}
\usepackage{titlesec}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\sectionfamily}{mm}
 {
  \fennell_section_family:nn { #1 } { #2 }
 }

\cs_new_protected:Npn \fennell_section_family:nn #1 #2
 {
  \tl_clear:N \l_fennell_section_font_tl
  \tl_clear:N \l_fennell_section_format_tl
  \str_case:nn { #2 }
   {
    { italic } { \tl_set:Nn \l_fennell_section_font_tl { \itshape } }
    { uppercase } { \tl_set:Nn \l_fennell_section_format_tl { \MakeUppercase } }
   }
  \use:x
   {
    \exp_not:N \titleformat
     { \exp_not:c { #1 } }
     { \exp_not:V \l_fennell_section_font_tl }
     { \exp_not:c { the#1 } }
     { 1em }
     { \exp_not:V \l_fennell_section_format_tl }
   }
 }
\ExplSyntaxOff

\sectionfamily{section}{uppercase}
\sectionfamily{subsection}{italic}

\begin{document}

\section{How does this look?}

\subsection{How does this look?}

\end{document}

示された例に基づいて他のキーワードを追加できます。ただし、この「自動化」で得られるメリットはそれほど多くないと思います。

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

関連情報