測試字串內的腳註

測試字串內的腳註

我想在測試父字串的命令中建立一個腳註(使用xstring),例如:

\documentclass{article}

\usepackage{etex}
\usepackage{xstring}

\begin{document}

\newcommand{\lymessage}{This is my\footnote{footnote text} message.}

\IfEndWith{\lymessage}{message}{It might be true.}{It is definitely false.}

\end{document}

我知道腳註一般來說很脆弱,但我嘗試過的每個變體都無法編譯。如果我把腳註去掉,它就像一個魅力。

我在網路上搜尋了答案,但沒有在任何地方找到這個確切的案例/問題,不幸的是,其他稍微相關的案例的幾個解決方法並不適用於此。

預先感謝您的任何幫助!如果我自己找到解決方案,我會跟進。

答案1

的標準設定xstring\fullexpandarg;根據您需要如何使用它,您可以

\documentclass{article}

\usepackage{xstring}
\noexpandarg % don't do full expansion in `purple' arguments

\begin{document}

\newcommand{\lymessage}{This is my\footnote{footnote text} message.}

\expandafter\IfEndWith\expandafter{\lymessage}{message}{It might be true.}{It is definitely false.}

\end{document}

文件(第 3.1.1 節)中以紫色列印的參數\noexpandarg不受擴充。為了擴展\lymessage(一次),我們需要\expandafter.

否則,您可以對\expandarg每個「紫色」參數中的第一個標記執行單一擴充步驟。

\documentclass{article}

\usepackage{xstring}
\expandarg % expand just once the `purple' arguments

\begin{document}

\newcommand{\lymessage}{This is my\footnote{footnote text} message.}

\IfEndWith{\lymessage}{message}{It might be true.}{It is definitely false.}

\end{document}

或者,如果您在其他地方需要\fullexpandarg,您可以在本地停用該功能:

\documentclass{article}

\usepackage{xstring}

\begin{document}

\newcommand{\lymessage}{This is my\footnote{footnote text} message.}

\saveexpandmode\expandarg
\IfEndWith{\lymessage}{message}{It might be true.}{It is definitely false.}
\restoreexpandmode

\end{document}

答案2

在此輸入影像描述

\documentclass{article}

\usepackage{etex}
\usepackage{xstring}

\begin{document}

\newcommand{\lymessage}{This is my\footnote{footnote text} message.}

\begingroup
\def\footnote#1{}%
\IfEndWith{\lymessage}{message}{It might be true.}{It is definitely false.}%
\endgroup

\lymessage

\end{document}

相關內容