將複雜的參數模式傳遞給新指令

將複雜的參數模式傳遞給新指令

我正在使用漂亮的changes包。其中基本上提供了三個命令\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}

相關內容