fncychap 和 pythontex 兼容性

fncychap 和 pythontex 兼容性

我尝试在同一个文档中使用 pythontex 和 fncychap 包,但显然存在某种不兼容性。

如果我尝试编译此文件:

\documentclass[11pt]{book}% 
\usepackage{fncychap}
\usepackage{pythontex}

\begin{document}
\chapter{Test}
This document uses Python
\end{document}

我收到以下错误: ! LaTeX Error: Command \py already defined.

我下载了 fncychap 源代码,想寻找类似的东西\newcommad{\py}{...},但我没有找到类似的东西。另一方面,fncychap 使用了\py多次。

我正在pdfTeX 3.14159265-2.6-1.40.20 (TeX Live 2019/Debian)从 Ubuntu 20.0 的 texlive 包运行

有什么想法吗?

答案1

它们都定义了\py

fncychap将其定义为长度,因此更难改变,这将 pythontex 重命名为\pythontexpy

\documentclass[11pt]{book}% 


\usepackage{fncychap}
\let\fncychappy\py
\let\py\undefined

\usepackage{pythontex}

\AtBeginDocument{%
\let\pythontexpy=\py
\let\py\fncychappy
}


\begin{document}

\typeout{\string\py: \meaning\py}
\typeout{\string\pythontexpy: \meaning\pythontexpy}
\chapter{Test}
This document uses Python
\end{document}

答案2

我写了一个稍微复杂一点的例子,以防有人发现它有用:

documentclass{book}% 

\usepackage{lipsum}

\usepackage[Sonny]{fncychap}
\let\fncychappy\py %% Store '\py' contents in '\fncychappy'
\let\py\undefined %% Undefine '\py' to avoid collision with pythontex

\usepackage{pythontex}

\AtBeginDocument{%
\let\pythontexpy=\py %% Store '\py' contents in '\pythontexpy'
\let\py\fncychappy %% Restore '\py' contents defined by fncychap.
}


\begin{document}

%% Send messages explaining the changes on the terminal and on the
%% log file.
\typeout{\string\py: \meaning\py}
\typeout{\string\pythontexpy: \meaning\pythontexpy}
%% Messages end.

%% Document body.
    \chapter{Test}
    \lipsum[1]\\

    Did you know that $2^{65} = \pythontexpy{2**65}$ ? %% Use '\pythontexpy' istead of '\py'
    
\end{document}

好吧,社区机器人似乎对这个简短的回复不太满意,所以我会添加一些细节。代码与 David Carlisle 在他的回答中写的代码基本相同。我添加了两件事:

  • fncychap 包中的章节样式选项(Sonny)显示其对章节显示方式的影响(默认样式与 LaTeX 默认样式没有区别)。
  • 使用该命令替换fncychap 包捕获的\pythontexpy命令的示例。\py

相关内容