為什麼在標題中使用參數時會加倍?

為什麼在標題中使用參數時會加倍?

我寫了一些命令,最近添加了一些方便的命令\FS來添加圖形來源:

\newcommand{\FS}[1]{%
\protect\\{\protect\scriptsize{}Bildquelle: #1}}

我不知道這是否正確(所有這些\protect),但它似乎在內部起作用\caption。不幸的是,我嘗試改進該命令,並添加一個可選參數:

\newcommand{\FS}[2][Bildquelle]{%
\protect\ifthenelse{\equal{#1}{}}%
\protect\\{\protect\scriptsize{}#2}
\protect\\{\protect\scriptsize{}#1: #2}}

當使用未指定可選參數的變體時,效果是先#2輸出擴展,然後再輸出擴展Bildquelle: #2(實際上,即使指定可選參數,輸出也會按描述重複)。例如:

顯示重複展開的 <code>#2</code> 的範例

我對 TeX 的了解還不夠深,無法自己解決問題,即使在閱讀了\protect.命令該怎麼寫?

答案1

注意\ifthenelse{<condition>}{<true>}{<false>}需要<true><false>進行分組。如果不是,則假定 後面的兩個標記<condition>表示<true><false>。在您的設定中,<true>is\protect\\is <false>。您可能正在尋找類似的東西

\newcommand{\FS}[2][Bildquelle]{%
  \protect\ifthenelse{\equal{#1}{}}%
    {\protect\\{\protect\scriptsize{}#2}}
    {\protect\\{\protect\scriptsize{}#1: #2}}}

使用條件測試如何檢查巨集值是否為空或不會使用純 TeX 條件建立文字?,您可以定義\FS

\newcommand{\FS}[2][Bildquelle]{%
  \\
  \scriptsize
  \if\relax\detokenize{#1}\relax\else
    #1: 
  \fi
  #2%
}

這是一個完整的最小使用範例:

在此輸入影像描述

\documentclass{article}

\usepackage{caption}

\newcommand{\FS}[2][Bildquelle]{%
  \\
  \scriptsize
  \if\relax\detokenize{#1}\relax\else
    #1: 
  \fi
  #2%
}

\begin{document}

\begin{figure}
  \caption{Some caption. \FS{abc}}
  \caption{Another caption. \FS[abc]{def}}
  \caption{Final caption. \FS[]{ghi}}
\end{figure}

\end{document}

相關內容