# comment.sty and UTF8 encoding

I noted that if I use comment.sty and a there is a block of text containing UTF8 characters inside a comment environment, the compilation aborts!

Minimal Example

\documentclass{article}
\usepackage[T1]{fontenc}
\RequirePackage[utf8]{inputenc}%this file is stored as UTF8 file!
\RequirePackage{comment}
\specialcomment{privateSolution}
{\begingroup\itshape\noindent\textbf{Solution}\\}
{\endgroup}
%\excludecomment{privateSolution}%Use it to remove solutions!

\begin{document}
\title{Comment.sty and UTF8 test file}
\maketitle
\section{UTF8 character test}
In which alphabet characters àìùòè are common?
\begin{privateSolution}
The Italian alphabet contains àìùòè.
\end{privateSolution}

\begin{comment}
The Italian one! It contains àìùòè.
\end{comment}
\end{document}


Has anyone the same problem? Any workaround?

-
Ciao! Avresti potuto semplicemente bussare sul muro! –  egreg Feb 11 at 13:47
No problems here (TeX Live2013). Perhaps an outdated package(s)? –  Gonzalo Medina Feb 11 at 13:52
I don't believe it is a problem of outdated package because I regularly update packages using TeX Live Utility in my Apple Mac (I currently use TeX Live 2013). Are you sure to use UTF8 enconding when you save the example file? –  Roberto Pose Feb 11 at 14:12
Then I am missing something. I don't seem to see any problems in the final output and the compilation flows with no errors. –  Gonzalo Medina Feb 11 at 14:13
RobertoPose you could teach @egreg the mechanics of asking a question on this site, he hasn't managed it yet. –  David Carlisle Feb 11 at 15:11

The problem is in how comments.sty writes out files; when you input à, it is interpreted during a \write and it becomes the character corresponding to à in the T1 encoding.

Solution: modify \ThisComment to write out uninterpreted commands.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}%this file is stored as UTF8 file!
\usepackage{comment}

\renewcommand\ThisComment[1]{%
}

\specialcomment{privateSolution}
{\itshape\noindent\textbf{Solution}\\}
{}
%\excludecomment{privateSolution}%Use it to remove solutions!

\begin{document}
\title{Comment.sty and UTF8 test file}
\maketitle
\section{UTF8 character test}
In which alphabet characters àìùòè are common?
\begin{privateSolution}
The Italian alphabet contains àìùòè.
\end{privateSolution}

\begin{comment}
The Italian one! It contains àìùòè.
\end{comment}
\end{document}


A completely different solution using environ:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}%this file is stored as UTF8 file!
\usepackage{environ}

\newif\ifsolution
\NewEnviron{privateSolution}{%
\ifsolution
\par\noindent\textbf{\textit{Solution}}\\
\BODY
\fi
}
%\solutiontrue % Uncomment it to print solutions!

\begin{document}
\title{Comment.sty and UTF8 test file}
\maketitle
\section{UTF8 character test}
In which alphabet characters àìùòè are common?
\begin{privateSolution}
The Italian alphabet contains àìùòè.
\end{privateSolution}

\end{document}

-
Thanks Enrico (egreg)! –  Roberto Pose Feb 11 at 15:11
The @egreg's second solution is surely very elegant but it works only when the body of environment does not contain verbatim text/environments as, for example, listings. –  Roberto Pose Feb 12 at 7:39
@RobertoPose Indeed, that's a limitation of environ: environments defined with \NewEnviron absorb the content of the environment as a macro argument, so verbatim material cannot be used in them. –  egreg Feb 12 at 8:51