내 매크로의 제목이 깨지기 쉬운 이유는 무엇입니까? 좀 더 단단하게 만들 수 있을까요?

내 매크로의 제목이 깨지기 쉬운 이유는 무엇입니까? 좀 더 단단하게 만들 수 있을까요?

다음 코드는 메시지가 무시되면 출력이 양호하더라도 컴파일을 중지합니다.

내 매크로가 깨지기 쉬운 이유는 무엇입니까? 여기를 좀 더 견고하게 만들 수 있을까요?

\documentclass[12pt,a4paper]{article}

\makeatletter
    \def\txtIf{\@ifstar\tnsalgo@txtIf@star\tnsalgo@txtIf@no@star}
    \def\tnsalgo@txtIf@no@star{\textbf{Si}}
    \def\tnsalgo@txtIf@star{\texttt{Si}}
\makeatother


\begin{document}

\subsubsection{Conditional: \protect\txtIf{}} % <-- OK

\subsubsection{Conditional: \txtIf{}}         % <-- KO

\end{document}

통나무 :

This is pdfTeX, Version 3.14159265-2.6-1.40.21 (TeX Live 2020) (preloaded format=pdflatex 2020.9.12)  12 SEP 2020 13:22
entering extended mode
 \write18 enabled.
 file:line:error style messages enabled.
 %&-line parsing enabled.
**pdftex-titel-pb.tex
(./pdftex-titel-pb.tex
LaTeX2e <2020-02-02> patch level 5
L3 programming layer <2020-09-06>
(/usr/local/texlive/2020/texmf-dist/tex/latex/base/article.cls
Document Class: article 2019/12/20 v1.4l Standard LaTeX document class
(/usr/local/texlive/2020/texmf-dist/tex/latex/base/size12.clo
File: size12.clo 2019/12/20 v1.4l Standard LaTeX file (size option)
)
\c@part=\count168
\c@section=\count169
\c@subsection=\count170
\c@subsubsection=\count171
\c@paragraph=\count172
\c@subparagraph=\count173
\c@figure=\count174
\c@table=\count175
\abovecaptionskip=\skip47
\belowcaptionskip=\skip48
\bibindent=\dimen134
)
(/usr/local/texlive/2020/texmf-dist/tex/latex/l3backend/l3backend-pdftex.def
File: l3backend-pdftex.def 2020-09-11 L3 backend support: PDF output (pdfTeX)
\l__kernel_color_stack_int=\count176
\l__pdf_internal_box=\box45
)
(./pdftex-titel-pb.aux)
\openout1 = `pdftex-titel-pb.aux'.

LaTeX Font Info:    Checking defaults for OML/cmm/m/it on input line 11.
LaTeX Font Info:    ... okay on input line 11.
LaTeX Font Info:    Checking defaults for OMS/cmsy/m/n on input line 11.
LaTeX Font Info:    ... okay on input line 11.
LaTeX Font Info:    Checking defaults for OT1/cmr/m/n on input line 11.
LaTeX Font Info:    ... okay on input line 11.
LaTeX Font Info:    Checking defaults for T1/cmr/m/n on input line 11.
LaTeX Font Info:    ... okay on input line 11.
LaTeX Font Info:    Checking defaults for TS1/cmr/m/n on input line 11.
LaTeX Font Info:    ... okay on input line 11.
LaTeX Font Info:    Checking defaults for OMX/cmex/m/n on input line 11.
LaTeX Font Info:    ... okay on input line 11.
LaTeX Font Info:    Checking defaults for U/cmr/m/n on input line 11.
LaTeX Font Info:    ... okay on input line 11.

./pdftex-titel-pb.tex:13: Argument of \@sect has an extra }.
<inserted text> 
                \par 
l.13 \subsubsection{Conditional: \txtIf{}}
                                          
? 
Runaway argument?
{\normalfont \normalsize \bfseries }{\@firstoftwo {\tnsalgo@txtIf@star \ETC.
./pdftex-titel-pb.tex:13: Paragraph ended before \@sect was complete.
<to be read again> 
                   \par 
l.13 \subsubsection{Conditional: \txtIf{}}
                                          
? 
[1

{/usr/local/texlive/2020/texmf-var/fonts/map/pdftex/updmap/pdftex.map}]
(./pdftex-titel-pb.aux) ) 
Here is how much of TeX's memory you used:
 378 strings out of 480876
 6653 string characters out of 5907597
 243537 words of memory out of 5000000
 16086 multiletter control sequences out of 15000+600000
 535693 words of font info for 31 fonts, out of 8000000 for 9000
 1141 hyphenation exceptions out of 8191
 24i,4n,25p,181b,114s stack positions out of 5000i,500n,10000p,200000b,80000s
</usr/local/texlive/2020/texmf-dist/fonts/type1/public
/amsfonts/cm/cmbx12.pfb></usr/local/texlive/2020/texmf-dist/fonts/type1/public/
amsfonts/cm/cmr12.pfb>
Output written on pdftex-titel-pb.pdf (1 page, 17436 bytes).
PDF statistics:
 16 PDF objects out of 1000 (max. 8388607)
 10 compressed objects within 1 object stream
 0 named destinations out of 1000 (max. 500000)
 1 words of extra memory for PDF output out of 10000 (max. 10000000)


답변1

명령을 "강력"하거나 "취약"하게 만드는 요인에 대한 자세한 내용은 다음을 참조하세요.Fragile 명령과 Robust 명령의 차이점은 무엇입니까?

\protect에서처럼 깨지기 쉬운 명령을 지정하는 것 외에도 \subsubsection{Conditional: \protect\txtIf{}}"이동" 명령의 인수에 문제의 명령을 배치하는 데 성공할 수 있는 (적어도) 두 가지 방법이 있습니다.

  • 또는 \DeclareRobustCommand대신 with를 정의하여 처음부터 명령을 강력하게 만듭니다 .\def\newcommand

  • etoolbox패키지를 로드하고 실행하여 명령이 생성된 후 강력한 명령을 만듭니다.

    \robustify{\fragilecommand}
    

    예: \robustify{\txtIf}.

관련 정보