titleformat 명령에 전달하기 위해 "section"을 "\section"으로 변환

titleformat 명령에 전달하기 위해 "section"을 "\section"으로 변환

\titleformat패키지 명령은 다음 titlesec과 같은 방식으로 호출됩니다.

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

나는 섹션 제목의 형식을 포함하여 일부 형식을 자동화하기 위해 작은 패키지를 작성 중이며, 이는 titlesec내가 "랩핑"하는 패키지 중 하나입니다. 문제는 내 코드를 다음 형식으로 작성하고 싶다는 것입니다.

\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

두 가지 진술이 있어야합니다 \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}

제공된 예를 기반으로 다른 키워드를 추가할 수 있습니다. 하지만 이 "자동화"로 많은 것을 얻을 수 있다고 생각하지는 않습니다.

여기에 이미지 설명을 입력하세요

관련 정보