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?

  • 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? – user36296 Dec 17 '18 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 Dec 17 '18 at 12:55
  • Please make a short compilable document, that includes all the necessary packages to compile your code. – user36296 Dec 17 '18 at 12:57
  • 4
    The python environment is not for printing Python code, but for executing it. – egreg Dec 17 '18 at 12:58
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

  • 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 8:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.