# Highlight punctuation marks in pdf

I would like to automatically highlight all punctuation in my document, ideally with a different background color, or by changing the color of the punctuation mark.

The aim is mostly for me to cross check my sentence length aand general style, so I'm not worried about dots in equations and/or headlines.

Is there an easy way for me to 'redefine' a dot as \color{red}{.} to turns all dots red?

Solution using the accepted answer below (because I can't make code work in the comments)

\usepackage{xcolor}
\usepackage{stringstrings}

\let\svcomma,\let\svperiod.\let\svsemicolon;
\catcode,=\active\catcode.=\active \catcode;=\active %
\newcommand\colorpuncton[1][red]{%
% \catcode,=\active\def,{\colorbox{#1}{\svcomma}}%
\catcode.=\active\def.{\colorbox{blue!30}{\svperiod}}%
\catcode;=\active\def;{\colorbox{green!30}{\svsemicolon}}%
}

\def\colorpunctoff{%
\catcode,=12\let ,\svcomma%
\catcode.=12\let .\svperiod%
\catcode;=12\let ;\svperiod%
}

\catcode,=12 \catcode.=12 \catcode;=12 %

\parskip 1em

\let\oldfigure\figure% Store old figure environment start
\let\endoldfigure\endfigure% Store old figure environment end
\renewenvironment{figure}[1][htbp]% Redefine figure
{\colorpunctoff\oldfigure[#1]}
{\endoldfigure\colorpuncton}
\let\oldtable\table%
\let\endoldtable\endtable%
\renewenvironment{table}[1][htbp]%
{\colorpunctoff\oldtable[#1]}
{\endoldtable\colorpuncton}

\let\oldSI\SI
\renewcommand{\SI}[2]{\colorpunctoff\oldSI{#1}{#2}\colorpuncton}


Based on my answer at Change the color of capital letters. The ability to turn it off might allow things like the period to be used in the normal way when needed.

EDITED to allow simple suspend/renew of the method, if one must call on some other macro requiring punctuation, such as \cite. RE-EDITED to allow this suspend/renew in a single macro call, \Xcite{}.

\documentclass{article}
\usepackage{xcolor}
\usepackage{stringstrings}

\let\svcomma,\let\svperiod.\let\svsemicolon;
\catcode,=\active \catcode.=\active \catcode;=\active %
\newcommand\colorpunctinit[1][red]{\colorpuncton%
\def,{\colorbox{#1}{\svcomma}}%
\def.{\colorbox{#1}{\svperiod}}%
\def;{\colorbox{#1}{\svsemicolon}}%
}
\newcommand\colorpuncton{%
\catcode,=\active %
\catcode.=\active %
\catcode;=\active %
}
\def\colorpunctoff{%
\catcode,=12 %
\catcode.=12 %
\catcode;=12 %
}
\newcommand\coloron[2]{%
\if\svcomma#1\catcode,=\active\def,{\colorbox{#2}{\svcomma}}\else%
\if\svperiod#1\catcode.=\active\def.{\colorbox{#2}{\svperiod}}\else%
\if\svsemicolon#1\catcode;=\active\def;{\colorbox{#2}{\svsemicolon}}\else%
\fi\fi\fi%
}
\newcommand\coloroff[1]{\catcode#1=12%
\edef\tmp{\detokenize{#1}}%
\caselower[q]{\tmp}%
\expandafter\let\expandafter#1\csname sv\thestring\endcsname%
}
\catcode,=12 \catcode.=12 \catcode;=12 %

\parskip 1em

\newcommand\Xcite{\colorpunctoff\citeX}
\newcommand\citeX[1]{\cite{#1}\colorpuncton}

\begin{document}
\colorpunctinit[cyan]
\verb|\colorpunctinit[color]| will change all punctuation to the specified color,\\
here cyan, and should be done to initialize this process.\\
(\verb|But not commas; for some reason, in \verbatim.|)

\colorpunctoff
\verb|\colorpunctoff| will, indeed, suspend the procedure.

\coloron{,}{red}\coloron{.}{blue!30}\coloron{;}{green!30}
\verb|\coloron{<punctuation>}{color}| will turn an inactive symbol active with the
specified color, done here  with red, to ",", blue!30 to the .'' and green!30 to the
;''.

\coloroff{,}
\verb|\coloroff{<punctuation>}|, done here to the comma,
will restore that letter to the original LaTeX
setting; the others remain active.

\coloron{,}{cyan!40}
To change the color an already active symbol, one must first turn the color off;
then set the color anew, as done here to the comma ,''.

To use \verb|\cite|, one must employ \verb|\Xcite{A, B}|
as in \Xcite{A, B}. The punctuation, it is still colored; yes it is.
\end{document}


• Thanks for the suggestion. Unfortunately, as you predicted, I need to disable the code for almost every graphic that's been included in the document (of about 100 pages). Would there be an easy way to escape that or do I need to disable it befoer each figure individually? – myrtille Aug 5 '16 at 13:44
• @myrtille You could perhaps revise the figure environment to disable upon entry and reenable upon exit. – Steven B. Segletes Aug 5 '16 at 13:52
• I managed to get most of it fixed. I tried to post the code, but it looked horrible. But I can't make the cite command work (it breaks if you use 2 or more citations in a single cite \cite{herrmann:1984, arnold:2005}. I'm using the cite package. if anyone could give me a tip on how to achieve that? So far I'm just not higlhighting the , which works as well. – myrtille Aug 6 '16 at 17:27
• @myrtille See my edit. I have redefined all the macros a bit to accommodate it. – Steven B. Segletes Aug 8 '16 at 10:25
• The challenge is in redefining the cite command to automatically disables & reeneables coloroff. It is not as easy as for the SI command unfortunately. – myrtille Aug 9 '16 at 12:12

You can make the period active and give it a definition. But you should be aware that periods are quite often part of the syntax e.g. of numbers, and that you can get quite horrible errors if the period is not what the syntax expects. So you will have to change the catcode again in such cases.

\documentclass{article}
\usepackage{xcolor}
\usepackage{tikz}
\begin{document}
\catcode\.=13
\def.{\colorbox{red}{\string.}}

a horse. a horse. a horse.

\catcode\.=12 %disable the colored period
\begin{tikzpicture}
\draw(0.5,0.5)--(1,1);
\end{tikzpicture}
\catcode\.=13

a kingdom.

\end{document}
`

• Thanks for the solution. Unfortunately I have the same issue here, as I have with the second solution offered: Is there a work around to not manually disable & reenable this for every figure in the text, as I usually give the size of the fiugre relative to the textwidth, the sizes all contain something similar to" 0.7\textwidth" which breaks. – myrtille Aug 5 '16 at 13:46