解决方案

解决方案

我正在尝试根据 todonotes 和 changes 包定义一个新的 \note 命令,但我在可选的选定文本样式方面遇到了麻烦。我希望它根据更改 ID 分配的颜色进行着色。

以下是代码:

\documentclass[12pt]{article}
\usepackage[dvipsnames,svgnames,x11names]{xcolor}
\usepackage[markup=underlined]{changes}
\usepackage[]{todonotes}

\definechangesauthor[color=BrickRed]{MyName}

\makeatletter
\newcommand{\note}[2][]{%
  \@ifnextchar[{\note@i[{#1}]{#2}}{\note@i[{#1}]{#2}[{}]}%
}
\def\note@i[#1]#2[#3]{%
  \todo[color=Changes@Color#1!20,size=\scriptsize]{#1: \emph{#2}} %
  \textcolor{Changes@Color#1}{#3} %
}
\makeatother

\begin{document}

Lorem ipsum dolor sit \note{Just a note without selected text and
without author.} amet, consectetur adipiscing elit, sed do eiusmod tempor 
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis 
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo 
consequat.\note[MyName]{My comment}{Proin a lectus vestibulum, mollis eros 
id, vehicula mauris.} Duis aute irure dolor in reprehenderit in voluptate 
velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat 
cupidatat \note[MyName]{My other comment without text selectd.}non 
proident, sunt in culpa qui officia deserunt mollit anim id est \note{Some 
comment about some selected text.}{laborum}

\end{document}

结果如下: 在此处输入图片描述

但我想要的文本是“Proin a lectus vestibulum, mollis eros id, vehicula mauris。”颜色为砖红色。

PS!我应该提一下,我使用了这个包像这样生成新命令:

$ python newcommand.py 
% Prototype: MACRO notee OPT[#1={}] #2 OPT[#3={}]
\makeatletter
\newcommand{\notee}[2][]{%
  \@ifnextchar[{\notee@i[{#1}]{#2}}{\notee@i[{#1}]{#2}[{}]}%
}

\def\notee@i[#1]#2[#3]{%
  % Put your code here.
  % You can refer to the arguments as #1 through #3.
}
\makeatother

解决方案

感谢@egreg!经过一些调整,我最终得到了我想要的结果!

\usepackage{xparse}

\NewDocumentCommand{\note}{omo}{%
  % #1 (optional) = addition to Changes@color
  % #2 text for \todo
  % #3 (optional) text for the change%
  \IfValueTF{#1}
    {
        \todo[color=Changes@Color#1!20,size=\scriptsize]{#1: \emph{#2}}
        \IfValueT{#3}{\textcolor{Changes@Color#1}{#3}}
    }%
    {
        \todo[color=Changes@Color!20,size=\scriptsize]{\emph{#2}}
        \IfValueT{#3}{\textcolor{Changes@Color}{#3}}
    }%
}

输出: 在此处输入图片描述

答案1

根据定义的语法,Proin...应该在括号中:

\documentclass[12pt]{article}
\usepackage[dvipsnames,svgnames,x11names]{xcolor}
\usepackage[markup=underlined]{changes}
\usepackage[]{todonotes}

\definechangesauthor[color=BrickRed]{MyName}

\makeatletter
\newcommand{\note}[2][]{%
  \@ifnextchar[{\note@i[{#1}]{#2}}{\note@i[{#1}]{#2}[{}]}%
}
\def\note@i[#1]#2[#3]{%
  \todo[color=Changes@Color#1!20,size=\scriptsize]{#1: \emph{#2}}%
  \textcolor{Changes@Color#1}{#3}%
}
\makeatother

\begin{document}

Lorem ipsum dolor sit \note{Just a note without selected text and
without author.} amet, consectetur adipiscing elit, sed do eiusmod tempor 
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis 
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo 
consequat.\note[MyName]{My comment}[Proin a lectus vestibulum, mollis eros 
id, vehicula mauris.] Duis aute irure dolor in reprehenderit in voluptate 
velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat 
cupidatat \note[MyName]{My other comment without text selectd.}non 
proident, sunt in culpa qui officia deserunt mollit anim id est \note{Some 
comment about some selected text.}{laborum}

\end{document}

在此处输入图片描述

也许更友好的定义方式\notexparse

\documentclass[12pt]{article}
\usepackage[dvipsnames,svgnames,x11names]{xcolor}
\usepackage[markup=underlined]{changes}
\usepackage[]{todonotes}
\usepackage{xparse}

\definechangesauthor[color=BrickRed]{MyName}

\NewDocumentCommand{\note}{O{}mo}{%
  % #1 (optional) = addition to Changes@color
  % #2 text for \todo
  % #3 (optional) text for the change
  \todo[color=Changes@Color#1!20,size=\scriptsize]{#1: \emph{#2}}%
  \IfValueT{#3}{\textcolor{Changes@Color#1}{#3}}%
}

\begin{document}

Lorem ipsum dolor sit \note{Just a note without selected text and
without author.} amet, consectetur adipiscing elit, sed do eiusmod tempor 
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis 
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo 
consequat.\note[MyName]{My comment}[Proin a lectus vestibulum, mollis eros 
id, vehicula mauris.] Duis aute irure dolor in reprehenderit in voluptate 
velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat 
cupidatat \note[MyName]{My other comment without text selectd.}non 
proident, sunt in culpa qui officia deserunt mollit anim id est \note{Some 
comment about some selected text.}{laborum}

\end{document}

相关内容