xparse및 etoolbox(“LaTeX2e 스타일”) 사용

xparse및 etoolbox(“LaTeX2e 스타일”) 사용

제목이 비어 있는 명령에 대해 다른 동작을 갖는 섹션 명령(예: 하위 단락)을 만들고 싶습니다.

이것은 내가 얻고자 하는 것에 대해 어느 정도 보여주는 내 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

xparseetoolbox(“LaTeX2e 스타일”) 사용

이 방법은 xparse및 를 사용합니다 etoolbox. 추가적으로, \@startsectionTeX가 이를 사용할 때 의 다섯 번째 인수가 반드시 확장된다는 사실을 이용합니다 . 빈 필수 인수를 \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}

스크린샷

xparseand expl3(“ expl3스타일”) 사용

다음 방법은 expl3LaTeX3 프로젝트의 언어인 의 기술을 사용합니다. 그것~할 수 있었다\@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}

출력은 위와 동일합니다.

관련 정보