2

I'm trying to use pythontex and fncychap packages in the same document but, apparently, there is some sort of incompatibility.

If I try to compile this file:

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

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

I get the following error: ! LaTeX Error: Command \py already defined.

I've downloaded the fncychap sources searching for something like \newcommad{\py}{...}, but I have not found anything like that. On the other hand, fncychap uses \py multiple times.

I'm running pdfTeX 3.14159265-2.6-1.40.20 (TeX Live 2019/Debian) from the texlive package of Ubuntu 20.0

Any thoughts?

2

2 Answers 2

3

they both define \py.

fncychap defines it as a length so is harder to change, this renames the pythontex one to \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}
1
0

I've written a slightly more complex example, just in case someone finds it useful:

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}

Well, the community bot doesn't seem very happy with this short reply, so I'll add some details. The code is basically the same that David Carlisle wrote in his answer. I've added two things:

  • A chapter style option (Sonny) to the fncychap package to show its effect on how the chapter is displayed (the default style is indistinguishable from LaTeX default).
  • An example of use of the command \pythontexpy which replaces the command \py captured by the fncychap package.
1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Aug 19, 2022 at 8:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .