3

I'm trying to easily plot with pgfplots some python outputs. I really don't like the matplotlib2tikz default behavior, which is to copy the default parameters of matplotlib, and I can't find how to override it. I would like to have my basic axis, so all the custom config would be in my \pgfplotsset{}.

So I did some basic functions in python, and I call them with sagetex. I like this package because I can load all the imports and access to the results anywhere in the document. It almost works perfectly, except with babel and the french options. Here is a MWE :

\documentclass[tikz]{standalone}
\usepackage{sagetex}
\usepackage{pgfplots}
  \pgfplotsset{compat=1.13}

\usepackage[french]{babel}     %<--- Any one of these make it fail
%\usepackage[frenchb]{babel}   %<--- This 
%\usepackage[francais]{babel}  %<--- This 

%\usepackage[english]{babel}   %<--- But this one is okay.

\begin{document}
%Import python function
\begin{sagesilent}

############## python code
def addplot_coordinates(*args, **kwargs):
  if len(kwargs) is 0:
    code = [r'\addplot coordinates {']
  else:
    code = [r'\addplot[']
    for key, value in kwargs.items():
      code.append('  {0} = {1},'.format(key, str(value)))
    code.append('  ] coordinates {')
  while True:
    try:
      value = [str(x.pop()) for x in args]
    except IndexError:
      break
    line = '    ( ' + ' , '.join(value) + ' )'
    code.append(line)
  code.append(r'} ;')
  return '\n'.join(code)

# Print a test in standard output during sage compilation to be sure it works
print(addplot_coordinates([1,3,2,4], [2,5,3,2], draw='black'))
############## end of python code

\end{sagesilent}

\begin{tikzpicture}
    \begin{axis}

      % This plot works fine
      \addplot[
        draw = blue,
        ] coordinates {
          ( 4 , 2 ) ( 2 , 1 ) ( 3 , 2 ) ( 1 , 1 )
      } ;

      % This plots fails
      \sagestr{addplot_coordinates([1,2,3,4], [4,3,4,3], draw='red')}

    \end{axis}
\end{tikzpicture}
\end{document}

Used without babel, or with the english option, it compiles (pdflatex, sage, pdflatex). But with one of the french options, I have this error during the 2nd pdflatex compilation:

Runaway argument?
 ;{}{}{}\null \fi \@safe@activesfalse \end {NoHyper}\@ifundefined {r@\ETC.
! File ended while scanning use of \pgfplots@addplotimpl@coordinates.
<inserted text> 
              \par 
<*> TestBug.tex

I have found something in the SageTex manual about the NoHyper environment (p. 22), saying that there have been a problem, but I don't understand it as if I should protect my code with something.

It may worth mentioning that everything is fine as long as :

  • I do not invoke a \addplot command
  • I use an other babel option

Does any one have a clue on what might happen ? Plus, I'm totally fine with coding this pgf-python interface, since it will give me a lot of control on what I do, but feel free to advise me a better way.

Thank you for you time !

  • see if you add \usetikzlibrary{babel} before or after\usepackage{pgfplots} helps to resolve your problem. – Zarko Sep 9 '17 at 19:34
  • As you suggested, I added \usetikzlibrary{babel} after \pgfplotsset{compat=1.13} and the errors disappeared. – DJP Sep 10 '17 at 2:17
  • Yes, it does solve the problem. Thank you. So I tried to understand why, and it was simply the ;. A \shorthandoff{;} removes the compilation fail. Should I add an answer ? – Oca Sep 10 '17 at 2:39
  • of course you should :), actually, you already done :-) :-) – Zarko Sep 10 '17 at 11:30
1

In fact, it was just a simple babel-tikz problem.

As suggested in the comments by Zarko, adding:

\usetikzlibrary{babel}

solves the problem.

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.