Reputation: 11
I was plotting as usual and then after saving one of my plots as a pgf and then compiling LaTeX it didn't work anymore. As far as I'm aware of, I didn't change anything it just so happen to stop working.
The code I'm using to make an example plot is the following:
import matplotlib . pyplot as plt
import numpy as np
from pathlib import Path
import matplotlib .cm as cm
import tikzplotlib
import matplotlib as mpl
mpl.rcParams['mathtext.fontset'] = 'cm' # Latex font
mpl.rcParams['font.family'] = 'serif'
mpl.rcParams['font.serif'] = ['cmr10']
mpl.rcParams['axes.formatter.use_mathtext'] = True
# Erzeuge eine Liste von x-Werten
x = np.linspace(-5, 10, 100)
a= [2,8,8,2]
b=[2,2,8,2]
U_1=2
U_2=8
I_1=2
I_2=8
# Berechne die entsprechenden y-Werte für die Funktion y = x
y = x
# Plotte die Funktion
plt.plot(x, y)
plt.plot(a,b, color = 'crimson', marker='', linestyle='dashed')
plt.plot(U_1,I_1, color = 'green', marker='x', linestyle='',label='$(U_{1}, I_{1})$', markersize=14)
plt.plot(U_2,I_2, color = 'orange', marker='x', linestyle='',label='$(U_{1}, I_{2})$', markersize=14)
plt.plot(U_2,I_1, color = 'purple', marker='x', linestyle='',label='$(U_{2}, I_{2})$', markersize=14)
# Beschriftungen für die Achsen hinzufügen
plt.xlabel('$I$ / arbitrary units')
plt.ylabel('$U$ / arbitrary units')
# Titel für den Plot hinzufügen
plt.title('Beispiel Fit')
plt.legend(loc='upper left')
# Grid hinzufügen
plt.grid(True)
# Den Plot anzeigen
plt.savefig('example.pgf', format='pgf', bbox_inches='tight', pad_inches = 0)
plt.show()
I tried multiple things and I'm pretty sure that the problem is in the commands
mpl.rcParams['font.serif'] = ['cmr10']
mpl.rcParams['axes.formatter.use_mathtext'] = True
Sure I can commen them out but then (more bovious for other plots) the axes will be in a weird font and if I have a different, let's say logarithmic scaling, it wont look like 10^{-21} like in latex and the font is different as well.
The error codes in Latex which I'm getting are
! Undefined control sequence.
<recently read> \mathdefault
l.109 ...\catcode`\%=\active\def%{\%}$\mathdefault
{\ensuremath{-}4}$}}%
The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.
! Undefined control sequence.
<recently read> \mathdefault
l.147 ...\catcode`\%=\active\def%{\%}$\mathdefault
{\ensuremath{-}2}$}}%
The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.
! Undefined control sequence.
<recently read> \mathdefault
l.185 ...\catcode`\%=\active\def%{\%}$\mathdefault
{0}$}}%
The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.
! Undefined control sequence.
<recently read> \mathdefault
l.223 ...\catcode`\%=\active\def%{\%}$\mathdefault
{2}$}}%
The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.
...
I also posted this on https://tex.stackexchange.com/q/718364/36296 in the hope to find a solution
Upvotes: 1
Views: 532
Reputation: 1
Reposting my answer in a similar GitHub thread https://github.com/matplotlib/matplotlib/issues/27907
I think I found a workaround. In the PGF file itself there is a helpful commented out preamble that has some instructions for including/importing the image.
%% Matplotlib used the following preamble
%% \def\mathdefault#1{#1}
%% \everymath=\expandafter{\the\everymath\displaystyle}
It also has the code needed to define "\mathdefault" so I copied this into my main ".tex" file, and it appeared to work.
Upvotes: 0
Reputation: 11
Update: After some more researching I am now certain that the problem occurs because the pgf file uses the command '\mathdefault' which LaTex doe not know. I manually deleted all of these commands in my .pgf file and now Latex is compiling as usual without any issue.
I wrote a small .bat script
@echo off
setlocal enabledelayedexpansion
for %%f in (*.pgf) do (
set "outfile=%%~nf_temp.pgf"
(
for /f "tokens=*" %%l in (%%f) do (
set "line=%%l"
set "line=!line:\mathdefault=!"
echo !line!
)
) > "!outfile!"
move /y "!outfile!" "%%f" > nul
)
endlocal
which deletes them automatically after the pgf file is created. Does someone know how I can do this directly in python without the need of this extra bat file ?
Upvotes: 0