将复杂的参数模式传递给新命令

将复杂的参数模式传递给新命令

我正在使用 nicechanges包。它基本上提供了三个命令\added\deleted\replaced。由于其结构和可选参数的事实,我认为可以简化其使用。我按照xparse包文档查看是否可以创建一个简化调用的命令。

下面是一个 MWE。我能够创建两个版本的命令,一个是,delete另一个是,replace具体取决于参数的数量。但我很快就筋疲力尽了。

总之,这里是我希望通过定义新命令实现的映射\ME

  • \ME{deleted}映射到\deleted[id=Me]{deleted}
  • \ME[added]映射到\added[id=Me]{added}(或至少等同于\ME[added]{}
  • \ME[replacing]{replaced}映射到\replaced[id=Me]{replacing}{replaced}
  • \ME[comment]{deleted}映射到\deleted[id=Me, remark={comment}]{deleted}
  • \ME[comment][added]映射到\added[id=Me,remark={comment}]{added}(或等同于\ME[comment][added]{}
  • \ME[comment][replacing]{replaced}映射到\replaced[id=Me, remark={comment}]{replacing}{replaced}

梅威瑟:

\documentclass{article}
\usepackage{changes}
\usepackage{xparse}

\definechangesauthor[name={MyName},color=blue]{ME}

\DeclareDocumentCommand \ME { o m }{%
  \IfNoValueTF{#1}{
    \deleted[id=ME]{#2}}{
    \replaced[id=ME]{#1}{#2}
}
}


%\newcommand{\ME}[1]{\deleted[id=ME]{#1}} %ok

%\newcommand{\ME}[1][]{\added[id=ME]{#1}} % not working

\begin{document}

\added[id=ME,remark={added}]{added} 

\deleted[id=ME,remark={deleted}]{deleted}

\replaced[id=ME,remark={replaced}]{replacing}{replaced}

\ME{aaa}       % ok
\ME[aaa]{bbb}  % ok
\ME[aaa]{}     % ok

\ME{remark}[aaa]{} 

\end{document}

编辑:到目前为止,总结一下。感谢@AxelBall 和@ChristianHupfer,我得到了这个语法,它几乎是理想的(除了纯注释),实现如下:

\ME{addonly}

\ME{add}<deleted>

\ME<deleteonly>

\ME[addonlycommet]{addonly}

\ME[adddeletecomment]{add}<delete>

\ME[deleteonlycomment]<deleteonly>

text\ME[commentonly]

id最后是星号变体,在修订版中添加:

\ME*[deleteonlycomment]<deleteonly>

代码:

\NewDocumentCommand{\ME}{sogd<>}{
\IfBooleanTF#1{
\IfNoValueTF{#2}{\IfNoValueTF{#3}{\IfNoValueF{#4}{\deleted[id=ME]{#4}}}{\IfNoValueTF{#4}{\added[id=ME]{#3}}{\replaced[id=ME]{#3}{#4}}}}{\IfNoValueTF{#3}{\IfNoValueTF{#4}{\added[id=ME,remark={#2}]{}}{\deleted[id=ME, remark={#2}]{#4}}}{\IfNoValueTF{#4}{\added[id=ME,remark={#2}]{#3}}{\replaced[id=ME, remark={#2}]{#3}{#4}}}}
}{
\IfNoValueTF{#2}{\IfNoValueTF{#3}{\IfNoValueF{#4}{\deleted{#4}}}{\IfNoValueTF{#4}{\added{#3}}{\replaced{#3}{#4}}}}{\IfNoValueTF{#3}{\IfNoValueTF{#4}{\added[remark={#2}]{}}{\deleted[remark={#2}]{#4}}}{\IfNoValueTF{#4}{\added[remark={#2}]{#3}}{\replaced[remark={#2}]{#3}{#4}}}}
}
}

答案1

\ME这里我们定义一个接受三个可选参数的命令:

\ME[optional comment]<deleted content>(added content)

必须给出和/或其中之一<deleted>(added)否则命令不执行任何操作(但不会引发错误/警告)。

已编辑来纠正替换行为。)

\documentclass{standalone}
\usepackage{changes}
\usepackage{xparse}

\definechangesauthor[name={MyName},color=blue]{ME}

\NewDocumentCommand{\ME}{od<>d()}{%
  \IfNoValueTF{#1}{%
    \IfNoValueTF{#2}{%
      \IfNoValueF{#3}{%
        % (#3)
        \added[id=ME]{#3}%
      }%
    }{%
      \IfNoValueTF{#3}{%
        % <#2>
        \deleted[id=ME]{#2}%
      }{% <#2>(#3)
        \replaced[id=ME]{#3}{#2}%
      }%
    }%
  }{%
    \IfNoValueTF{#2}{%
      \IfNoValueF{#3}{%
        % [#1](#3)
        \added[id=ME,remark={#1}]{#3}%
      }%
    }{%
      \IfNoValueTF{#3}{%
        % [#1]<#2>
        \deleted[id=ME, remark={#1}]{#2}%
      }{% [#1]<#2>(#3)
        \replaced[id=ME, remark={#1}]{#3}{#2}%
      }%
    }%
  }%
}

\begin{document}

\begin{tabular}{ll}
\deleted[id=ME]{deleted} 
& \ME<deleted> \\
\added[id=ME]{added}
& \ME(added) \\
\replaced[id=ME]{replacing}{replaced}
& \ME<replaced>(replacing)\\
\deleted[id=ME, remark={comment}]{deleted}
& \ME[comment]<deleted> \\
\added[id=ME,remark={comment}]{added}
& \ME[comment](added)\\
\replaced[id=ME, remark={comment}]{replacing}{replaced}
& \ME[comment]<replaced>(replacing)\\
\end{tabular}
\end{document}

第一个代码示例的输出

如果您担心使用括号,请不要担心。xparse如果在第三个参数内使用平衡括号,则可以很好地应对;如果您想要不平衡的括号,请将其括在括号中:\ME(like {(} this)

如果您确实想对最后一个参数使用括号,则可以使用g说明符:

\NewDocumentCommand{\ME}{od<>g}{...}

编辑

好的,这应该可以满足你的要求:

\documentclass[border=12pt]{standalone}
\usepackage{changes}
\usepackage{xparse}

\definechangesauthor[name={MyName},color=blue]{ME}

\NewDocumentCommand{\ME}{ogd<>}{%
  \IfNoValueTF{#1}{%
    \IfNoValueTF{#2}{%
      \IfNoValueF{#3}{%
        % <#3>
        \deleted[id=ME]{#3}%
      }%
    }{%
      \IfNoValueTF{#3}{%
        % {#2}
        \added[id=ME]{#2}%
      }{% {#2}<#3>
        \replaced[id=ME]{#2}{#3}%
      }%
    }%
  }{%
    \IfNoValueTF{#2}{%
      \IfNoValueTF{#3}{%
        % [1]
        \added[id=ME,remark={#1}]{}%
      }{% [#1]<#3>
        \deleted[id=ME, remark={#1}]{#3}%
      }%
    }{%
      \IfNoValueTF{#3}{%
        % [#1]{#2}
        \added[id=ME,remark={#1}]{#2}%
      }{% [#1]{#2}<#3>
        \replaced[id=ME, remark={#1}]{#2}{#3}%
      }%
    }%
  }%
}

\begin{document}

\begin{tabular}{ll}
\verb|\ME<deleted>|
& \ME<deleted> \\
\verb|\ME{added}|
& \ME{added} \\
\verb|\ME{added}<deleted>|
& \ME{added}<deleted>\\
\verb|\ME[comment]|
& \ME[comment]\\
\verb|\ME[comment]<deleted>|
& \ME[comment]<deleted> \\
\verb|\ME[comment]{added}|
& \ME[comment]{added}\\
\verb|\ME[comment]{added}<deleted>|
& \ME[comment]{added}<deleted>\\
\end{tabular}

\end{document}

输出:

第二个代码示例的输出

答案2

这实际上使用了三个可选参数:

\ME[operation driver]<operation argument>[operation comment]

其中任何一个都可以省略,在这种情况下什么也不会发生。

\documentclass{article}
\usepackage{changes}
\usepackage{xstring}
\usepackage{xparse}

\definechangesauthor[name={MyName},color=blue]{ME}

\DeclareDocumentCommand \MEOrig{ o m }{%
  \IfNoValueTF{#1}{
    \deleted[id=ME]{#2}}{
    \replaced[id=ME]{#1}{#2}
}
}



\DeclareDocumentCommand{\ME}{oD<>{}o}{%
  \IfNoValueTF{#1}{%
    \IfValueTF{#3}{%
      \deleted[id=ME,remark={#3}]{#2}%
    }{%
      \deleted[id=ME]{#2}%
    }%  
  }{%
    \IfStrEq{#1}{added}{%
      \IfValueTF{#2}{%
        \IfValueTF{#3}{%
          \added[id=ME,remark={#3}]{#2}%
        }{%
          \added[id=ME]{#2}%
        }%
      }{%
        \IfValueTF{#3}{%
          \added[id=ME,remark={#3}]{}%
        }{%
          \added[id=ME]{}%
        }%
      }%
    }{% 
      \IfStrEq{#1}{replacing}{%
        \IfValueTF{#3}{%
          \replaced[id=ME,remark={#3}]{replacing}{#2}%
        }{%
          \replaced[id=ME]{replacing}{#2}%
        }%
      }{%
        % Do nothing in here
      }%      
    }% End of IfStrEq{#1}{added}
  }% End of IfNoValueTF{#1}
}







\begin{document}



\added[id=ME,remark={added}]{added} 

\deleted[id=ME,remark={deleted}]{deleted}

\replaced[id=ME,remark={replaced}]{replacing}{replaced}

\ME<aaa>       % ok
\ME[aaa]<bbb>  % ok
\ME[aaa]     % ok

\ME<remark>[aaa]

\end{document}

相关内容