# Change what is displayed in comments in the code?

I recognize that LaTeX is unlikely to be able to do this, so this probably boils down to "Is there any third-party LaTeX editing software that..."

As a college instructor, I write exercise sets for my students that become quite lengthy. In order to find my way around the documents easily, I try to include appropriate labels and explanations in comments. For example

% Question 5: Differentiate a product
\item <question>
<worked out solution>

% Question 6: Differentiate a quotient
\item <question>
<worked out solution>


The problem is when I have a long list (50 or more questions) and I decide to add some questions into the middle of the list. Now my numbering in the comments is off. In order to fix them, I have to go through the entire file manually changing each one.

So, is there a way to define some kind of counter or tag that will allow me to drop in new questions and have the comments renumber? Again, this is when I'm looking at the code, so that I see:

% Question 5: Differentiate a product
\item <question>
<worked out solution>

% Question 6: NEW QUESTION
\item <question>
<worked out solution>

% Question 7: Differentiate a quotient
\item <question>
<worked out solution>


And the comments in the code have renumbered themselves.

• I'm not sure exactly what you want to achieve. Do you mean to typeset the comments separately, in some sort of appendix, with the numbers referring to the questions? Maybe have a look at the answers package. – Michael Palmer Dec 20 '16 at 14:33
• @MichaelPalmer My reading is that HTG types in the new question, and subsequent numbering in the comments of the .tex file automatically updates. HTG: The only reason I can think of for wanting to do this is so that you can find which source corresponds to which output. Is that your intent? Because the showlabels package could help accomplish the same thing. – Teepeemm Dec 20 '16 at 14:49
• Can you explain why you need the question numbers in your latex file comments? I would probably write an AWK script to achieve your objective. The script would step through the latex file searching for lines beginning with % Question and change the question index numbers so they properly increment. – James Dec 20 '16 at 14:49
• I don't know that I "need" the question numbers in the comments. I tend to think in terms of "Question 1" and such, so the comments reflect this mental organization. Many of them aren't labeled, so the showlabels package would be of limited use, at least initially. And no, I'm not intending to typeset the comments. – HTG Dec 20 '16 at 15:41
• I've been thinking about this more as I work. I have a list of say 50 exercises. To keep track of where I'm at in the list, each one is preceded by a comment: % Ex. 1 or % Ex. 5 or something. When I want to drop a new exercise into the middle of the list, or delete one, the enumerate environment renumbers the typesetting automatically. But now the comments in the code aren't accurate. Over time, they become more inaccurate until finally I manually renumber the comments. This question was a faint hope there was an automatic way to do that. Thanks for listening! – HTG Jan 7 '17 at 18:44

This is an AWK solution, not a LaTeX solution, so maybe it should be moved to another forum.

If you have an AWK or GAWK interpreter on your system, you can use the following script to automatically renumber your source file.

BEGIN { questionNum = 1; }
{
if ($1 == "%" &&$2 == "Question")
{
out = $1" "$2" "questionNum":";
for (i = 4; i <= NF; i++) { out = out" "$i; } print out; ++questionNum; } else { print$0; }
}


ORIGINAL FILE:

% Question 5: Differentiate a product
\item <question>
<worked out solution>

% Question x: NEW QUESTION
\item <question>
<worked out solution>

% Question 7: Differentiate a quotient
\item <question>
<worked out solution>


PROCESSED FILE:

% Question 1: Differentiate a product
\item <question>
<worked out solution>

% Question 2: NEW QUESTION
\item <question>
<worked out solution>

% Question 3: Differentiate a quotient
\item <question>
<worked out solution>


You can do this as a 'one-liner' with perl; for example, you can use:

perl -p -e 's/(?<!\\)%(\h*Question\h*)\d+/$i++;"%$1$i"/e' htg.tex  where htg.tex is, for example: % Question 5: Differentiate a product \item <question> <worked out solution> % Question 6: NEW QUESTION \item <question> <worked out solution> % Question 7: Differentiate a quotient \item <question> <worked out solution> %Question 4: Differentiate a quotient \item <question> <worked out solution> %Question5: Differentiate a quotient \item <question> <worked out solution> this one has a percentage symbol, \% Question 5: Differentiate a quotient \item <question> <worked out solution> %Question6: Differentiate a quotient \item <question> <worked out solution>  The output when (carefully!, at your own risk) using perl -p -i.bak -e 's/(?<!\\)%(\h*Question\h*)\d+/$i++;"%$1$i"/e' htg.tex is

% Question 1: Differentiate a product
\item <question>
<worked out solution>

% Question 2: NEW QUESTION
\item <question>
<worked out solution>

% Question 3: Differentiate a quotient
\item <question>
<worked out solution>

%Question 4: Differentiate a quotient
\item <question>
<worked out solution>

%Question5: Differentiate a quotient
\item <question>
<worked out solution>

this one has a percentage symbol, \%     Question 5: Differentiate a quotient
\item <question>
<worked out solution>

%Question6: Differentiate a quotient
\item <question>
<worked out solution>


A few notes:

• -p tells perl to process the file (htg.tex) line by line, and print the output
• -e is the execute flag that allows us to run the perl code within the quotes
• -i.bak is only to be used once you're confident the thing works; it overwrites htg.tex and creates htg.tex.bak as a backup.
• s/this/that/ substitutes this with that.
• (?<!\\)%(\h*Question\h*)\d+ means a %, not pre-ceeded by a \, and then followed by zero or more horizontal spaces, then Question, then zero or more spaces, then one or more digits; the () are 'capturing' parenthesis, the result of which is stored in the special variable $1; -$i++;"%$1$i" increments a counter, $i, and then, finally, outputs % together with the result of $1, and the new value of \$i;
• the /e modifier at the end allows us to do the stuff in the replacement (extended mode).