You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在LaTeX Beamer中使用listings插入代码?编译超时问题求助

解决LaTeX Beamer中listings代码块编译超时问题

你的编译超时问题是因为lstlisting环境内的不规则缩进空行触发了listings包的解析死循环bug——当代码块里存在缩进不一致的空行时,listings的解析器会陷入无限循环,导致编译卡住。

以下是几种可行的解决办法:

  • 调整代码块内的缩进,保持统一格式,避免不规则的缩进空行:
    \begin{lstlisting}
    

Why
doesn't
this work ??
\end{lstlisting}

- 给listings添加`breaklines=true`参数,强制开启换行解析,绕过死循环逻辑:
可以在导言区全局设置,对所有`lstlisting`生效:
```latex
\lstset{breaklines=true}

也可以在单个lstlisting环境中局部设置:

\begin{lstlisting}[breaklines=true]
    Why 
        doesn't 
    this work ??
\end{lstlisting}
  • 更新listings包到最新版本,该bug在较新的包版本中已经被官方修复。

附上可正常编译的完整示例代码:

\documentclass{beamer}
\usepackage{graphicx, listings, tikz, subcaption, amsmath} 
\usetheme{Copenhagen}
\setbeamertemplate{footline}[frame number]
\lstset{breaklines=true}

\begin{document}

\maketitle

\begin{frame}{My frame}
    \begin{lstlisting}
        Why 
            doesn't 
        this work ??
    \end{lstlisting}
\end{frame}

\end{document}

内容的提问来源于stack exchange,提问作者Alphasaft

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 17:14:55