在 lstlisting 中插入 url 并使用 make4ht 进行编译时出现问题

在 lstlisting 中插入 url 并使用 make4ht 进行编译时出现问题

以下示例显示了在lstlisting标题中插入 URL 时出现的错误。

\documentclass{article}
\usepackage{listings}
\usepackage{hyperref}
\begin{document}
\begin{lstlisting}[language=tex, caption={Test (\url{http://www.test.org})}]
\LaTeX{}
\end{lstlisting}
\end{document}

编译时发现以下错误make4ht

[ERROR]   htlatex: Compilation errors in the htlatex run
[ERROR]   htlatex: Filename Line    Message
[ERROR]   htlatex: ./article.tex    5    Illegal parameter number in definition of \@tempa.
[ERROR]   htlatex: ./article.tex    5    Illegal parameter number in definition of \@tempa.
[ERROR]   htlatex: ./article.tex    5    Undefined control sequence.

日志文件显示以下消息:

! Illegal parameter number in definition of \@tempa.
<to be read again>
                   1
l.5 ...caption={Test (\url{http://www.test.org})}]

You meant to type ## instead of #, right?
Or maybe a } was forgotten somewhere earlier, and things
are all screwed up? I'm going to assume that you meant ##.

答案1

\protect该选项tex4ht将解决该问题:

\documentclass{article}
\usepackage{listings}
\usepackage[tex4ht]{hyperref}
\begin{document}
\begin{lstlisting}[language=tex, caption={Test (\protect\url{http://www.test.org})}]
\LaTeX{}
\end{lstlisting}
\end{document}

相关内容