我有一个接收一些参数的命令,例如:
\newcommand{\myappend}[1]{
\ifthenelse{\equal{#1}{aaaBB}}{\dothis}{\dothat}
}
假设参数为 'aaa'。如何将 'BB'(在命令内部)附加到 #1,即如何编辑 #1 ?
答案1
我不确定我是否理解了这个问题。这里有两个选项:第一个,使用辅助命令添加“BB”,#1
第二个做同样的事情,但使用包\StrSubstitute
中的命令xstring
:
\documentclass{article}
\usepackage{ifthen}
\usepackage{xstring}
\def\dothis{this}
\def\dothat{that}
\newcommand\myappend[1]{%
\def\x{#1BB}%
\ifthenelse{\equal{\x}{aaaBB}}{\dothis}{\dothat}%
}
\newcommand\myappendi[1]{%
\StrSubstitute{#1}{aaa}{aaaBB}[\x]%
\ifthenelse{\equal{\x}{aaaBB}}{\dothis}{\dothat}%
}
\begin{document}
\myappend{aaa}\ \myappend{aaaBB}
\myappendi{aaa}\ \myappendi{aaaBB}
\end{document}
答案2
来自:\ifstrequal
etoolbox
\documentclass{article}
\usepackage{etoolbox}
\newcommand{\myappend}[1]{%
\ifstrequal{#1}{aaaBB}{}{aaaBB}
}%
\begin{document}
\myappend{aaa}
\end{document}
和
\documentclass{article}
\usepackage{ifthen}
\newcommand{\myappend}[1]{%
\ifthenelse{\equal{#1}{aaaBB}}{}{aaaBB}
}%
\begin{document}
\myappend{aaa}
\end{document}
答案3
通过原语可以轻松进行 pdfTeX 下的字符串比较\pdfstrcmp
:
\documentclass{article}
% \pdfstrcmp{<string1>}{<string2>}
% \pdfstrcmp compares two strings and expands to 0 if the strings are equal, to -1 if the first string
% ranks before the second, and to 1 otherwise
\newcommand{\myappend}[1]{%
\ifnum\pdfstrcmp{#1}{aaa}=0
This% Strings are equal
\else
That% Strings are not equal
\fi%
}
\begin{document}
\myappend{aab} % That
\myappend{aaA} % That
\myappend{aaa} % This
\myappend{AAa} % That
\newcommand{\aaa}{aaa}
\myappend{\aaa} % This
\end{document}