17

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?

Thanks in advance.

  • Ciao! Avresti potuto semplicemente bussare sul muro! – egreg Feb 11 '14 at 13:47
  • No problems here (TeX Live2013). Perhaps an outdated package(s)? – Gonzalo Medina Feb 11 '14 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 '14 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 '14 at 14:13
  • 2
    RobertoPose you could teach @egreg the mechanics of asking a question on this site, he hasn't managed it yet. – David Carlisle Feb 11 '14 at 15:11
  • @DavidCarlisle ask Roberto to also teach you, you don't seem to have read the manual either for that ;-) –  Feb 11 '14 at 16:45

2 Answers2

21

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]{%
  \immediate\write\CommentStream{\unexpanded{#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}
egreg
  • 1,121,712
  • 1
    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 '14 at 07:39
  • 1
    @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 '14 at 08:51
  • The `unexpanded' command is part of e-TeX http://tex.stackexchange.com/questions/140785/how-is-unexpanded-defined so I can't very well use it as standard. Correct me if I'm wrong. – Victor Eijkhout Jul 16 '16 at 21:44
  • @VictorEijkhout LaTeX has been using e-TeX for several years. – egreg Jul 16 '16 at 22:36
  • Every single distribution? Anyway, testing for the presence of eTeX is easy enough, and an updated comment.sty is now on CTAN. – Victor Eijkhout Aug 26 '16 at 15:54
  • @VictorEijkhout e-TeX has been the default LaTeX engine for several years. Of course, several users may still be running on old distributions, but then they'll probably don't need UTF-8 support. – egreg Aug 26 '16 at 16:09
2

With my apologies for tardiness -- I just don't do much TeX anymore lately -- version 3.8 of comment.sty has been uploaded and approved on CTAN. That should fix this problem. Much thanks to @egreg for the solution.

Victor Eijkhout
  • 749
  • 6
  • 12