带有条件的用户命令中的不对称行为

带有条件的用户命令中的不对称行为

我经常使用用户定义的命令来突出显示乳胶文档中的修改。

\providecommand{\ifeqthenelse}[4]{\edef\tempa{#1}\def\tempb{#2}\ifx\tempa\tempb {#3} \else {#4}\fi}

\providecommand{\changed}[5]{{\ifeqthenelse{#3}{#5}{{\color{black}#1}}{\color{#4}#1}}%
{\ifeqthenelse{#3}{#5}{}{\color{grey}\ifeqthenelse{#2}{}{}{(}#2\ifeqthenelse{#2}{}{}{)~}}}%
{\ifeqthenelse{#3}{#5}{}{\ifeqthenelse{#3}{}{}{[}{\it\color{#4}#3}\ifeqthenelse{#3}{}{}{]~}}}}

\providecommand{\mychanged}[3]{\changed{#1}{#2}{#3}{blue}{accept}}

最后一个命令 mychanged 实际上用于进行修改。它的第一个参数是新文本,第二个参数是旧文本。最后,第三个参数由其他人使用,通过提供 accept 来接受我的修改。现在我发现 mychanged 的​​行为在前两个参数中是不对称的,具体如下:我可以在第一个参数中使用各种复杂的 latex,如方程式、引文和参考文献,而在第二个参数中,引文和参考文献通常会导致 latex 错误。因此,我总是需要修改旧文本。第二个参数输入 ifeqthenelse 命令的条件,所以我怀疑这就是它不起作用的原因。是否有合适的 ifeqthenelse 替代品,例如,是否有更好的方法来构建 if-then-else 条件,以便在字符串比较中接受引文等?请注意,最好避免使用包,尤其是如果它们不是独立于平台/发行版的。

这是一个完整的示例。它可以工作,但如果您在文档环境中取消注释第二行,它将停止工作。

\documentclass[a4paper,10pt]{article}

\usepackage{color}

\definecolor{blue}{rgb}{0,0,0.5}
\definecolor{grey}{rgb}{0.5,0.5,0.5}

\providecommand{\ifeqthenelse}[4]{\edef\tempa{#1}\def\tempb{#2}\ifx\tempa\tempb {#3} \else {#4}\fi}

\providecommand{\changed}[5]{{\ifeqthenelse{#3}{#5}{{\color{black}#1}}{\color{#4}#1}}%
{\ifeqthenelse{#3}{#5}{}{\color{grey}\ifeqthenelse{#2}{}{}{(}#2\ifeqthenelse{#2}{}{}{)~}}}%
{\ifeqthenelse{#3}{#5}{}{\ifeqthenelse{#3}{}{}{[}{\it\color{#4}#3}\ifeqthenelse{#3}{}{}{]~}}}}

\providecommand{\mychanged}[3]{\changed{#1}{#2}{#3}{blue}{accept}}

\begin{document}

\section{test}

This is a \mychanged{cite \cite{Fukugita:1986hr}}{}{}.
%This is a \mychanged{}{cite \cite{Fukugita:1986hr}}{}.

\bibliographystyle{apsrev}
\begin{thebibliography}{1}
\expandafter\ifx\csname natexlab\endcsname\relax\def\natexlab#1{#1}\fi
\expandafter\ifx\csname bibnamefont\endcsname\relax
  \def\bibnamefont#1{#1}\fi
\expandafter\ifx\csname bibfnamefont\endcsname\relax
  \def\bibfnamefont#1{#1}\fi
\expandafter\ifx\csname citenamefont\endcsname\relax
  \def\citenamefont#1{#1}\fi
\expandafter\ifx\csname url\endcsname\relax
  \def\url#1{\texttt{#1}}\fi
\expandafter\ifx\csname urlprefix\endcsname\relax\def\urlprefix{URL }\fi
\providecommand{\bibinfo}[2]{#2}
\providecommand{\eprint}[2][]{\url{#2}}

\bibitem[{\citenamefont{Fukugita and Yanagida}(1986)}]{Fukugita:1986hr}
\bibinfo{author}{\bibfnamefont{M.}~\bibnamefont{Fukugita}} \bibnamefont{and}
  \bibinfo{author}{\bibfnamefont{T.}~\bibnamefont{Yanagida}},
  \bibinfo{journal}{Phys. Lett. B} \textbf{\bibinfo{volume}{174}},
  \bibinfo{pages}{45} (\bibinfo{year}{1986}).
\end{thebibliography}

\end{document}

当前版本中吞掉的空格的图示(“a”和“test”之间没有空格,但如果有就更好了)

\documentclass[a4paper,10pt]{article}

\usepackage{color}

\definecolor{blue}{rgb}{0,0,0.5}
\definecolor{grey}{rgb}{0.5,0.5,0.5}

\newcommand{\printifnonempty}[2]{\if\relax\detokenize{#1}\relax\else#2\fi}
\newcommand{\changed}[5]{%
  \def\temp{#3}%
  \def\accept{#5}%
  \ifx\temp\accept
    #1
  \else
    \textcolor{#4}{\printifnonempty{#1}{#1}}%
    \textcolor{grey}{\printifnonempty{#2}{(#2)}}%
    \textcolor{#4}{\printifnonempty{#3}{[#3]}}%
  \fi
}

\begin{document}

\section{Test}

This is a\changed{ test}{ old}{comment}{blue}{accept}.

\end{document}

答案1

错误出现在\edef定义的 开头\ifeqthenelse。将其更改为\def

然而,有一个更简洁的方法可以做到这一点:

\def\accept{accept}
\newcommand{\mychanged}[3]{%
  \def\temp{#3}%
  \ifx\temp\accept
    #1
  \else
    \textcolor{blue}{\printifnonempty{#1}{#1}}%
    \textcolor{grey}{\printifnonempty{#2}{(#2)}}%
  \fi
}
\newcommand{\printifnonempty}[2]{\if\relax\detokenize{#1}\relax\else#2\fi}

相关内容