MS Word-style Comments
I think it would have been better to focus your MWE and your questions in separate questions to enable both you and the persons answering to provide focus comments.
The answer to your first question is that you need to add the (%) to ensure that no redundant empty spaces occur.
This corrects the first problem.
\newcommand{\comment}[3][akm]{%
% initials of the author (optional) + note in the margin
\ifdraft{\refstepcounter{akmctr}%
{%
\setstretch{1.0}% line spacing
\todo[color={red!100!green!33},size=\small,fancyline]{%
\textbf{Comment [\uppercase{#1} \arabic{akmctr}]:}~#2}
\ifthenelse{\equal{#3}{}}{}{\highlight[red!100!green!33]{#3}}%
}%
}{}%
}
See also this https://tex.stackexchange.com/a/19927/963.
I must also congratulate you on what looks like a very nicely set of to-do notes. I will come back and add the rest of the answers unless you want to split them as per my suggestion and please reduce your minimal by 80%.
How do we add counters for more users? Firstly, we need to define a method to add a user, as well as decide on the type of datastructure to hold the user names.
A comma delimited list would be a suitable data structure and one which we can manipulate via LaTeX kernel commands or the etoolbox package or even write our own commands to do so. This list is simply defined in a macro:
\def\users{yiannis,egreg,martin}
To add more users, we can just manually enter another name or create another command for it, opting for the
latter we use the LaTeX kernel \g@addto@macro
command to do so.
\def\adduser#1{\g@addto@macro{\users}{,#1}}
To add a user use \adduser{Mary}
and if you want to print the list of users, just expand the list by typing \users
.
The next step is to create the counters automatically, since we have a list we will use a for-loop, within a macro we call \counterfactory
.
\def\counterfactory#1{\@for\next:=#1\do{%
\ifcsname c@\next\endcsname%
\else
\newcounter{\next}%
\setcounter\next{0}
\fi
}}
\AtBeginDocument{\counterfactory\users}
When LaTeX allocates a counter for example foo
the counter it creates is c@counter
. The \ifcsname
part of the code in the for-loop just checks if the counter was created earlier and if not then goes on and creates the counter and sets it to zero.
Including the above code fragments in your preamble and adjusting the relevant parts of your \comment
, macro such as \ifdraft{\refstepcounter{akmctr}
to \ifdraft{\refstepcounter{#1}
etc would do the trick. Here is a minimal to see the counter part operating on its own.
\documentclass{article}
\makeatletter
\def\users{egreg,martin}
\def\adduser#1{\g@addto@macro{\users}{,#1}}
\def\counterfactory#1{\@for\next:=#1\do{%
\ifcsname c@\next\endcsname%
\else
\newcounter{\next}%
\setcounter\next{0}
\fi
}}
% activate at beginning of document
\AtBeginDocument{\counterfactory\users}
% add a few users
\adduser{aaron}
\adduser{yiannis}
\adduser{george}
\begin{document}
This documented commented by \users. \\
\stepcounter{yiannis}
\theyiannis\\% 1
\theaaron\\ %0
\setcounter{aaron}{100}\\ %100
\theaaron\\
\end{document}
One could use a similar technique to add a specific user color. We can modify the \adduser
macro to take two parameters, the first would be the user name and the second to be the color.
\def\adduser#1#2{%
\g@addto@macro{\users}{,#1}%
\expandafter\def\csname #1@color\endcsname{#2}
}
Here is another MWE to test the changes.
\documentclass{article}
\usepackage{xcolor}
\makeatletter
\def\users{egreg,martin}
\def\adduser#1#2{%
\g@addto@macro{\users}{,#1}%adds user to list
\expandafter\def\csname #1@color\endcsname{#2} %holds the color name in `\name@color`
}
\def\counterfactory#1{\@for\next:=#1\do{%
\ifcsname c@\next\endcsname%
\else
\newcounter\next%
\setcounter\next{0}
\fi
}}
\AtBeginDocument{\counterfactory\users}
\adduser{aaron}{red}
\adduser{yiannis}{blue}
\adduser{george}{orange}
\begin{document}
This documented commented by \users. \\
\stepcounter{yiannis}
\theyiannis\\% 1
\theaaron\\ %0
\setcounter{aaron}{100}\\ %100
\theaaron\\
\color{\yiannis@color} This text is printed in blue,
whereas Aaron's color is printed in \color{\aaron@color} red.
\end{document}
So where you say \highlight[red!100!green!33]
, you can replace with the user's color command. This would hopefully help you iterate your code and get what you want, as well as understand a bit better what went wrong.
The todonotes package sounds like it does what you want.
\usepackage{todonotes}
This is some text.\todo{This is a margin note with a line}
Here's more text.\todo[noline]{Here's a note in the margin without a line pointing to the text}