我需要制作一个涉及许多特殊字符的散点图,包括 LaTeX 特殊字符,如$
、\
和%
。为此,我使用 tikz。我首先使用 matplotlib 在 Python 中生成图,如下所示:
然后我用tikzplotliba
生成 .tex 文件。 、b
、c
和的图输出*
如下%
所示:
% This file was created by tikzplotlib v0.8.2.
\begin{tikzpicture}
\definecolor{color0}{rgb}{0.12156862745098,0.466666666666667,0.705882352941177}
\definecolor{color1}{rgb}{0.172549019607843,0.372549019607843,0.537254901960784}
\definecolor{color2}{rgb}{0.925490196078431,0,0.549019607843137}
\begin{axis}[
hide x axis,
hide y axis,
tick pos=left,
xmin=-3.11107425768857, xmax=4.04556696239392,
ymin=-1.96452826220114, ymax=3.30474420528924
]
\addplot [only marks, draw=color0, fill=color0, colormap/viridis]
table{%
x y
-1.05731886163643 -1.72501587731521
-2.78577238404846 -0.843765364632213
3.72026508875381 -1.22034965712686
0.00465397081060324 0.723899078670965
0.118172186120475 3.06523182040332
};
\draw[] (axis cs:-1.05731886163643,-1.72501587731521) -- (axis cs:-1.05731886163643,-1.72501587731521);
\node at (axis cs:-1.05731886163643,-1.72501587731521)[
scale=0.5,
text=color1,
rotate=0.0
]{a};
\draw[] (axis cs:-2.78577238404846,-0.843765364632213) -- (axis cs:-2.78577238404846,-0.843765364632213);
\node at (axis cs:-2.78577238404846,-0.843765364632213)[
scale=0.5,
text=color1,
rotate=0.0
]{b};
\draw[] (axis cs:3.72026508875381,-1.22034965712686) -- (axis cs:3.72026508875381,-1.22034965712686);
\node at (axis cs:3.72026508875381,-1.22034965712686)[
scale=0.5,
text=color1,
rotate=0.0
]{c};
\draw[] (axis cs:0.00465397081060324,0.723899078670965) -- (axis cs:0.00465397081060324,0.723899078670965);
\node at (axis cs:0.00465397081060324,0.723899078670965)[
scale=0.5,
text=color2,
rotate=0.0
]{*};
\draw[] (axis cs:0.118172186120475,3.06523182040332) -- (axis cs:0.118172186120475,3.06523182040332);
\node at (axis cs:0.118172186120475,3.06523182040332)[
scale=0.5,
text=color2,
rotate=0.0
]{%};
\end{axis}
\end{tikzpicture}
我已经尝试使用 转义特殊字符\verb||
。我通过在散点图中用\verb|<some char>|
而不是注释来围绕源中的字符来实现此目的<some char>
:
结果(唯一的区别是节点内容):
% This file was created by tikzplotlib v0.8.2.
\begin{tikzpicture}
\definecolor{color0}{rgb}{0.12156862745098,0.466666666666667,0.705882352941177}
\definecolor{color1}{rgb}{0.925490196078431,0,0.549019607843137}
\begin{axis}[
hide x axis,
hide y axis,
tick pos=left,
xmin=-2.21660561653058, xmax=3.61518365126537,
ymin=-2.10679459204322, ymax=3.24590880736972
]
\addplot [only marks, draw=color0, fill=color0, colormap/viridis]
table{%
x y
-1.90642120048988 0.361422280176018
3.35010232091101 -1.86348989206991
-1.95152428617622 -0.405029504344616
1.63436899897306 3.0026041073964
-1.12652583321798 -1.0955069911579
};
\draw[] (axis cs:-1.90642120048988,0.361422280176018) -- (axis cs:-1.90642120048988,0.361422280176018);
\node at (axis cs:-1.90642120048988,0.361422280176018)[
scale=0.5,
text=color1,
rotate=0.0
]{\verb|a|};
\draw[] (axis cs:3.35010232091101,-1.86348989206991) -- (axis cs:3.35010232091101,-1.86348989206991);
\node at (axis cs:3.35010232091101,-1.86348989206991)[
scale=0.5,
text=color1,
rotate=0.0
]{\verb|b|};
\draw[] (axis cs:-1.95152428617622,-0.405029504344616) -- (axis cs:-1.95152428617622,-0.405029504344616);
\node at (axis cs:-1.95152428617622,-0.405029504344616)[
scale=0.5,
text=color1,
rotate=0.0
]{\verb|c|};
\draw[] (axis cs:1.63436899897306,3.0026041073964) -- (axis cs:1.63436899897306,3.0026041073964);
\node at (axis cs:1.63436899897306,3.0026041073964)[
scale=0.5,
text=color1,
rotate=0.0
]{\verb|*|};
\draw[] (axis cs:-1.12652583321798,-1.0955069911579) -- (axis cs:-1.12652583321798,-1.0955069911579);
\node at (axis cs:-1.12652583321798,-1.0955069911579)[
scale=0.5,
text=color1,
rotate=0.0
]{\verb|%|};
\end{axis}
\end{tikzpicture}
但这并没有什么帮助。
在生成 tikz 代码之前,有没有一种简单的方法可以在源代码中生成有效的转义 LaTeX 字符串?我想避免事后手动修复生成的代码。但如果没有其他办法,那么也可以。