4

I am trying to define a new \note command based on todonotes and changes packages but I'm having troubles with the optional selected text styling. I want it to be coloured based on the changes ID assigned colour.

Heres the code:

\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}

The result is looking like this: enter image description here

but I want the text "Proin a lectus vestibulum, mollis eros id, vehicula mauris." to be coloured BrickRed.

PS! I should mention that I used this package to generate the new command like so:

$ 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

Solution

Thanks to @egreg! With a bit of tweaking I ended up with the result I was looking for!

\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}}
    }%
}

Output: enter image description here

  • Welcome to TeX.SX! Nice example! – egreg Jan 13 '18 at 16:15
4

According to the defined syntax, Proin... should be in brackets:

\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}

enter image description here

A perhaps more friendly way to define \note with xparse:

\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}
  • @sandert I added a better way to define \note. – egreg Jan 13 '18 at 16:34
  • Please look at my modified solution. I thought I had it but there's a catch. If I set the todonotes package 'disable' option and changes package 'final' option then the text is still coloured. I dont want that :( – sandert Jan 13 '18 at 17:49
  • @sandert That's a completely different problem. – egreg Jan 13 '18 at 17:51
  • Should I make an another question then? :) – sandert Jan 13 '18 at 17:56
  • @sandert I'd say so – egreg Jan 13 '18 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.