2

I use the Python environment from package pythonhighlight for highlighting code syntax. However, the code is always framed. Is there a possibility to switch off the frame around the code?

Here is my code

\documentclass{article}
%some packages are loaded
\usepackage{pythonhighlight} %package to highlight the Python syntax

\begin{document}

\section{Python code}
\begin{python}
# Importing standard Qiskit libraries and configuring account
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit import execute, Aer, IBMQ
\end{python}
\end{document}

After compilation, the page look like this:

Enter image description here

I would like to get rid of the frame around the code.

3
  • 1
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Commented Jun 17, 2022 at 9:25
  • 1
    Always show some code so that people can see what you tried and what can be modifed. It's called a Minimal Working Example (it has to be compilable).
    – Miyase
    Commented Jun 17, 2022 at 10:28
  • @Miyase: Thank you for the feedback. I improved my question.
    – user249017
    Commented Jun 17, 2022 at 12:06

2 Answers 2

3

Package pythonhilight seems to be a very thin (and quite useless) wrapper around listings. It doesn't even have a documentation. So I suggest that you use listings directly.

The following example includes some configuration that you can change to suit your needs:

  • The \lstset command includes the Python syntax highlighting rules that I've been using.
  • In the arguments of the lstlisting environment, remove the numbers option to remove line numbers on the left.
  • Remove the frame option to remove the frame (this is what you asked for).
\documentclass{article}
\usepackage{xcolor}
\usepackage[procnames]{listings}

% Custom highlight rules for Python
\definecolor{keywords}{RGB}{255,0,90}
\definecolor{comments}{RGB}{0,0,113}
\definecolor{red}{RGB}{160,0,0}
\definecolor{green}{RGB}{0,150,0}
\lstset{language=Python, 
    basicstyle=\ttfamily\small, 
    keywordstyle=\color{keywords},
    commentstyle=\color{comments},
    stringstyle=\color{red},
    showstringspaces=false,
    identifierstyle=\color{green},
    procnamekeys={def,class},
    tabsize=4
}


\begin{document}

\section{Python code}

\begin{lstlisting}[
    language=Python,
    frame=single,   %Remove this option
    numbers=left]
# Importing standard Qiskit libraries and configuring account
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit import execute, Aer, IBMQ
\end{lstlisting}
    
\end{document}

Here is what you get after removing the last two options: result

0
0

Currently python environment provided by pythonhighlight package is defined as

\lstnewenvironment{python}[1][]{\lstset{style=mypython}}{}

which seems buggy: this environment takes one optional argument but never used it. Note the style mypython contains frame=trbl, which is the cause of frame around code.

I believe the expected definition should be

\lstnewenvironment{python}[1][]{\lstset{style=mypython,#1}}{}

A straight solution is to redefine lst-environment python, but it's actually harder because listings provides no way to do env redefinition. Thus the easier option is to define a new lst-environment similarly, with a new name:

\lstnewenvironment{pythonx.y.z}[1][]{\lstset{style=mypython,#1}}{} % just for example

\begin{pythonx.y.z}[frame=none]
<python code>
\end{pythonx.y.z}

Here is an example showing an attempt to provide \lstrenewenvironment then redefine python environment:

\documentclass{article}
%some packages are loaded
\usepackage{pythonhighlight} %package to highlight the Python syntax

\makeatletter
\lst@UserCommand\lstrenewenvironment#1#2#{%
  \@ifundefined{#1}%
    {\PackageError{Listings}{Environment `#1' undefined}\@eha
     \@gobbletwo}\relax
  \expandafter\let\csname#1\endcsname\relax
  \expandafter\let\csname#1@\endcsname\relax
  \expandafter\let\csname end#1\endcsname\relax
  \let\lst@arg\@empty
  \lst@XConvert{#1}\@nil
  \expandafter\lstnewenvironment@\lst@arg{#1}{#2}}

\lstrenewenvironment{python}[1][]{\lstset{style=mypython,#1}}{}
\makeatother

\begin{document}
\section{Python code}
\begin{python}[frame=none]
# Importing standard Qiskit libraries and configuring account
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit import execute, Aer, IBMQ
\end{python}
\end{document}

enter image description here

You must log in to answer this question.