1

i'm very new to latex and i'm trying to define a new environment that uses a existing environment and does some extra stuff at the beginning and the end. The environment i'm talking about is the "python" environment from the python package so i can highlight python code.

I defined my environment as the following:

\newenvironment{excall}
{\begin{python}}
{\end{python}}

and used it that way:

\begin{excall}
print("Hello")
print("1234")
\end{excall}

But getting this error:

! LaTeX Error: \begin{excall} on input line 54 ended by \end{document}.

if i try the same with the center environment instead of python, it works. What am i doing wrong?

4
  • Defining new environments that contain fragile content, such as code, is tricky. Can you please make a minimal working example (MWE) that gives us something to start with? Commented Dec 17, 2018 at 12:48
  • I'm not sure how to make it more simple than it already is. You can replace the 'excall' on my usage example with 'python' and see what it does if you have installed the package. It should color the 'print' in blue, the text in green and change the font.
    – Neralem
    Commented Dec 17, 2018 at 12:55
  • Please make a short compilable document, that includes all the necessary packages to compile your code. Commented Dec 17, 2018 at 12:57
  • 4
    The python environment is not for printing Python code, but for executing it.
    – egreg
    Commented Dec 17, 2018 at 12:58

1 Answer 1

1

The python environment from the python package is not for showing Python code, but for executing it.

Thus

\begin{python}
print("Hello")
print("1234")
\end{python}

will print

Hello 1234

If you want to highlight Python code, you can use the minted package. The igor style seems to choose the colors you like.

Beware that minted needs pdflatex to be run with the -shell-escape option.

enter image description here

\documentclass{article}
\usepackage{minted}

\usemintedstyle{igor}

\begin{document}

\begin{minted}{python}
print("Hello")
print("1234")
\end{minted}

\end{document}

enter image description here

You can customize minted, for instance

\documentclass{article}
\usepackage{minted}

\usepackage{lipsum} % for context

\usemintedstyle{igor}

\newenvironment{excall}
 {\VerbatimEnvironment\subsection*{Example call}\begin{minted}{python}}
 {\end{minted}\vspace{-\medskipamount}}

\begin{document}

\lipsum[4]

\begin{excall}
print("Hello")
print("1234")
\end{excall}

\lipsum[5]

\end{document}

enter image description here

6
  • Hey, thanks very much. Hmm...strange. It worked for me. Is it possible to wrap the version you mentioned in a new own environment? Cause i have to do some layout stuff before and after i show python code.
    – Neralem
    Commented Dec 17, 2018 at 13:23
  • @Neralem minted is customizable in various aspects. What do you want to add?
    – egreg
    Commented Dec 17, 2018 at 13:25
  • \subsubsection*{Example Call} at the beginning and \vspace{[-3mm]} at the end. And what do you mean with -shell-escape and pdflatex? I'm using Miktex cause i'm on windows.
    – Neralem
    Commented Dec 17, 2018 at 13:30
  • @Neralem I added an example of customization. For enabling shell escape, knowing the front-end you use is necessary; is it TeXStudio or what?
    – egreg
    Commented Dec 17, 2018 at 14:10
  • That looks exactly like what i wanted :) Thank you very much :) I'm using TeXworks. Is that a preference option? Cant find anything like that...even with google :(
    – Neralem
    Commented Dec 18, 2018 at 8:40

You must log in to answer this question.

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