0

I am trying to draw MP backgrounds around Python codes ; I want to automatically call some macros before/after the \startpython/\stoppython pair, so I define another environment to setup/clean. I already used that successfully with my other setups (not with vimtyping environments, but with Lists or TikZ...), but here I get this error :

"tex error > tex error on line 1 in file /opt/context/tex/texmf-context/tex/context/base/mkiv/cont-yes.mkiv: ! TeX capacity exceeded, sorry [input stack size=10000]"

I reproduced this behavior in the MWE below ; is there something I didn't see in the manuals ?

\usemodule[vim]
\definevimtyping[python][
  syntax=python
]
%% these are temps, the aim is to add MP background around
\def\startPythonCode{  
  \startpython
}
\def\stopPythonCode{
  \stoppython
}

\starttext
\startpython               %% This works, no pb
  print("Hello World")
\stoppython

\startPythonCode
  print("Hello World")    %% pb here : TeX capacity exceeded
\stopPythonCode
\stoptext

Thank you in advance,

======= EDIT ======

As it was asked in the coments, I add the MP background I intend to use :

\startuseMPgraphic{mp:codeFrame}
  numeric u,o ; u := 1EmWidth ; o := BodyFontSize;
  for i=1 upto nofmultipars :
    pair bl, br, ul, ur;
    bl := llcorner multipars[i]; br := lrcorner multipars[i];
    ul := ulcorner multipars[i]; ur := urcorner multipars[i];
    path p;
    p := ( bl -- br -- ur -- ul  -- cycle ) enlarged (0,0.5u) shifted (0,0);
    draw p withpen pencircle scaled 1bp withcolor 0.5 white;
  endfor;
\stopuseMPgraphic

\definetextbackground[codeFrameBackground][
  mp=mp:codeFrame,
  location=always,
  leftoffset=1em,rightoffset=1em,
  topoffset=0ex,bottomoffset=0ex,
  before={},
  after={},
  level=-2,
]

Thank you in advance,

====== EDIT 2 =======

The before/after pair, as suggested in DG's answer, does not seem to work with my real environment. Here is a new MWE, closer to my real environment, and which reproduces a "Missing } inserted" error.

In the /home/adrien/Enseignement-physique/tex/context/ConTeXt_adrienLicari_code.tex file :

\usemodule[vim]

%% Backgrounds
\startuseMPgraphic{mp:codeFrame}
  numeric u,o ; u := 1EmWidth ; o := BodyFontSize;
  for i=1 upto nofmultipars :
    pair bl, br, ul, ur;
    bl := llcorner multipars[i]; br := lrcorner multipars[i];
    ul := ulcorner multipars[i]; ur := urcorner multipars[i];
    path p;
    p := ( bl -- br -- ur -- ul  -- cycle ) enlarged (0,0.5u) shifted (0,0);
    draw p withpen pencircle scaled 1bp withcolor 0.5 white;
  endfor;
\stopuseMPgraphic

\definetextbackground[codeFrameBackground][
  mp=mp:codeFrame,
  location=always,
  leftoffset=1em,rightoffset=1em,
  topoffset=0ex,bottomoffset=0ex,
  before={},
  after={},
  level=-2
]

%% Python
\definevimtyping[python][
  syntax=python,
  strip=yes,
  tab=4,
  margin=1em,
  left={}, right={},
  numbering=yes,
  directory=../tmp-vimout/,
  before={\startcodeFrameBackground},
  after={\stopcodeFrameBackground}
]

In the mwe.tex file :

%% Environnement personnalisé
\usepath[{/home/adrien/Enseignement-physique/tex/context}]
\environment ConTeXt_adrienLicari_code

\starttext
\startpython
  print('Hello, world!')  # Fonction d'affichage
\stoppython
\stoptext

Thank you in advance,

4
  • 1
    Can't you just use before= and after= for that? github.com/adityam/filter/blob/master/…
    – DG'
    Commented Jul 15, 2021 at 13:32
  • I also tried, and got the same error. Commented Jul 15, 2021 at 13:48
  • Maybe you could add the MP code you want to use
    – DG'
    Commented Jul 15, 2021 at 14:31
  • I'll add it as an edit, even if I don't think it is necessary since the error is the same... Commented Jul 15, 2021 at 18:48

1 Answer 1

3

You can use the before= and after= options:

\usemodule[vim]

\startuseMPgraphic{mp:codeFrame}
  numeric u,o ; u := 1EmWidth ; o := BodyFontSize;
  for i=1 upto nofmultipars :
    pair bl, br, ul, ur;
    bl := llcorner multipars[i]; br := lrcorner multipars[i];
    ul := ulcorner multipars[i]; ur := urcorner multipars[i];
    path p;
    p := ( bl -- br -- ur -- ul  -- cycle ) enlarged (0,0.5u) shifted (0,0);
    draw p withpen pencircle scaled 1bp withcolor 0.5 white;
  endfor;
\stopuseMPgraphic

\definetextbackground
  [codeFrameBackground]
  [mp=mp:codeFrame,
   location=paragraph,
   leftoffset=1em,rightoffset=1em,
   topoffset=0ex,bottomoffset=0ex,
   before={},
   after={},
   level=-2]

\definevimtyping
  [python]
  [syntax=python,
   before={\startcodeFrameBackground},
   after={\stopcodeFrameBackground}]

\starttext

\startpython               %% This works, no pb
  print("Hello World")
\stoppython


\stoptext

enter image description here

The culprit in your updated MWE is the whitespace after after={\stopcodeFrameBackground}. To avoid this problem, you should write your setups either ending the last line with a closing bracket like this:

\setupsomething
  [whatever] 
  [key-1=value-1,
   key-2=value-2]

or with a comma, like that:

\setupsomething[whatever][
  key-1=value-1,
  key-2=value-2,
]

The first one is, btw, more context-y...

5
  • 1
    This is the way!
    – Aditya
    Commented Jul 17, 2021 at 23:23
  • Hello, Thank you for your answer ; however, it works for the MWE but not in my real environment. In this case I get a "Missing } inserted" error, This is why I tried the "define your wrapper environment" approach... Commented Jul 19, 2021 at 11:42
  • @A.Licari – That is why I asked for your code. Please modify your MWE so it reflects what you are actually doing
    – DG'
    Commented Jul 19, 2021 at 11:54
  • @A.Licari – See my update
    – DG'
    Commented Jul 19, 2021 at 13:23
  • Perfect, thank you ! Commented Jul 19, 2021 at 13:37

You must log in to answer this question.

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