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?

Neralem
  • 11

1 Answers1

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

egreg
  • 1,121,712
  • 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 Dec 17 '18 at 13:23
  • @Neralem minted is customizable in various aspects. What do you want to add? – egreg Dec 17 '18 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 Dec 17 '18 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 Dec 17 '18 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 Dec 18 '18 at 08:40
  • @Neralem see https://tex.stackexchange.com/questions/187276/ – egreg Dec 18 '18 at 08:47