comment.sty 和 UTF8 编码

comment.sty 和 UTF8 编码

我注意到,如果我使用 comment.sty,并且在注释环境中有一块包含 UTF8 字符的文本,则编译将中止!

最小示例

\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}

有人遇到过同样的问题吗?有什么解决方法吗?

提前致谢。

答案1

问题在于如何comments.sty写出文件;当你输入时à,它在 a 期间进行解释,并成为T1 编码中\write对应的字符。à

解决方案:修改\ThisComment为写出未解释的命令。

\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}

一个完全不同的解决方案使用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}

答案2

抱歉,我来晚了——我最近不怎么用 TeX 了——comment.sty 3.8 版已上传并在 CTAN 上获得批准。这应该可以解决这个问题。非常感谢 @egreg 提供的解决方案。

相关内容