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.

6
  • Ciao! Avresti potuto semplicemente bussare sul muro!
    – egreg
    Feb 11, 2014 at 13:47
  • No problems here (TeX Live2013). Perhaps an outdated package(s)? Feb 11, 2014 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? Feb 11, 2014 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. Feb 11, 2014 at 14:13
  • 2
    RobertoPose you could teach @egreg the mechanics of asking a question on this site, he hasn't managed it yet. Feb 11, 2014 at 15:11

2 Answers 2

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}
6
  • 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. Feb 12, 2014 at 7: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, 2014 at 8:51
  • The `unexpanded' command is part of e-TeX tex.stackexchange.com/questions/140785/… so I can't very well use it as standard. Correct me if I'm wrong. Jul 16, 2016 at 21:44
  • @VictorEijkhout LaTeX has been using e-TeX for several years.
    – egreg
    Jul 16, 2016 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. Aug 26, 2016 at 15:54
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.