符号 x 坐标中的西里尔文字

符号 x 坐标中的西里尔文字

我使用 pgfplots 制作条形图:

\documentclass{report}

\usepackage[T2A]{fontenc} % Поддержка русских букв
\usepackage[utf8]{inputenc} % Кодировка utf8
\usepackage[english, russian]{babel} % Языки: русский,

\usepackage{pgfplots}
\pgfplotsset{
every axis legend/.append style={at={(0.5,-0.13)},anchor=north,legend cell align=left},
tick label style={font=\tiny\scriptsize},
legend style={font=\scriptsize},
axis lines=left,
legend style={draw=none},
}
\begin{document}

\begin{figure} [H]
\centering
\begin{tikzpicture}
\begin{axis} [
ylabel={$T$},
ybar,
enlarge x limits=.15,
symbolic x coords={wer,good,neutral,not good,poor},
xtick=data,
bar shift=0pt
]

\addplot coordinates {(wer,1) (good,8)
(neutral,2) (not good,0) (poor,10)};

\end{axis}
\end{tikzpicture}
\caption{Прочность металлоизделий}
\end{figure}

\end{document}

并且它有效。 在此处输入图片描述

但是当我在symbolic x coords:中使用西里尔字符时symbolic x coords={сейчас,good,neutral,not good,poor},,编译时出现错误。

\documentclass{report}

\usepackage[T2A]{fontenc} % Поддержка русских букв
\usepackage[utf8]{inputenc} % Кодировка utf8
\usepackage[english, russian]{babel} % Языки: русский,

\usepackage{pgfplots}
\pgfplotsset{
every axis legend/.append style={at={(0.5,-0.13)},anchor=north,legend cell align=left},
tick label style={font=\tiny\scriptsize},
legend style={font=\scriptsize},
axis lines=left,
legend style={draw=none},
}
\begin{document}

\begin{figure} [H]
\centering
\begin{tikzpicture}
\begin{axis} [
ylabel={$T$},
ybar,
enlarge x limits=.15,
symbolic x coords={сейчас,good,neutral,not good,poor},
xtick=data,
bar shift=0pt
]

\addplot coordinates {(сейчас,1) (good,8)
(neutral,2) (not good,0) (poor,10)};

\end{axis}
\end{tikzpicture}
\caption{Прочность металлоизделий}
\end{figure}

\end{document}

错误:

Missing \endcsname inserted ]
Missing \endcsname inserted (neutral,2) (not good,0) (poor,10)};
Missing \endcsname inserted (neutral,2) (not good,0) (poor,10)};
Use of \pgfp@symb@coords@x@ doesn't match its definition (neutral,2) (not good,0) (poor,10)};
Missing \endcsname inserted (neutral,2) (not good,0) (poor,10)};
Missing \endcsname inserted (neutral,2) (not good,0) (poor,10)};
Extra \endcsname (neutral,2) (not good,0) (poor,10)};
Extra \endcsname (neutral,2) (not good,0) (poor,10)};
Illegal parameter number in definition of \pgfmathresult (neutral,2) (not good,0) (poor,10)};
Illegal parameter number in definition of \pgfmathresult (neutral,2) (not good,0) (poor,10)};
Illegal parameter number in definition of \pgfmathresult (neutral,2) (not good,0) (poor,10)};
Illegal parameter number in definition of \pgfmathresult (neutral,2) (not good,0) (poor,10)};
Illegal parameter number in definition of \pgfmathresult (neutral,2) (not good,0) (poor,10)};
Illegal parameter number in definition of \pgfmathresult (neutral,2) (not good,0) (poor,10)};
Illegal parameter number in definition of \pgfmathresult (neutral,2) (not good,0) (poor,10)};
Illegal parameter number in definition of \pgfmathresult (neutral,2) (not good,0) (poor,10)};
Illegal parameter number in definition of \pgfmathresult (neutral,2) (not good,0) (poor,10)};
Illegal parameter number in definition of \pgfmathresult (neutral,2) (not good,0) (poor,10)};
running in backwards compatibility mode (unsuitable tick labels; missing features). Consider writing \pgfplotsset{compat=1.8} into your preamble.

在其他地方,西里尔文不会导致错误。

如何解决?

答案1

符号坐标列表有两种用途:打印名称和以后引用它们。第二种用法是问题所在:符号坐标的基本功能good

\def\PREFIXgood{1}

(其中PREFIX是一些字符串,与讨论无关)以便宏可以替换(good,8)

(\PREFIXgood,8)

该值1是自动分配的,符号坐标列表从零开始。此过程使用\csname(见\csname 和 \endcsname 到底起什么作用?),不幸的是,西里尔字母在此上下文中不合法。例如,字母 ч 必须重新映射到它在 T2A 编码中分配到的位置,因此 LaTeX 首先将其转换为\cyrch,但现在此命令必须执行一些在 中不合法的操作\csname。在此上下文中,只有纯字母数字 ASCII 字符才是合法的。

有两种解决方法:第一种是使用xticklabels和中的相应数字\addplot coordinates

\begin{tikzpicture}
\begin{axis} [
  ylabel={$T$},
  ybar,
  enlarge x limits=.15,
  xticklabels={сейчас,good,neutral,not good,poor},
  xtick=data,
  bar shift=0pt
]
\addplot coordinates {(0,1) (1,8) (2,2) (3,0) (4,10)};
\end{axis}
\end{tikzpicture}

第二种解决方法更加激进:使用 LuaLaTeX(或 XeLaTeX):在这种情况下,西里尔字母会变成像其他字母一样的字符,并且在 中是合法的\csname

\documentclass[a4paper]{report}

\usepackage[english,russian]{babel} % Языки: русский,

\usepackage{fontspec}
\setmainfont[Ligatures=TeX]{CMU Serif}

\usepackage{pgfplots}
\pgfplotsset{compat=1.9}

\pgfplotsset{
  every axis legend/.append style={
    at={(0.5,-0.13)},
    anchor=north,
    legend cell align=left,
  },
  tick label style={font=\tiny\scriptsize},
  xticklabel style={
    anchor=base,
    yshift=-\baselineskip
  },
  legend style={font=\scriptsize},
  axis lines=left,
  legend style={draw=none},
}

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis} [
  ylabel={$T$},
  ybar,
  enlarge x limits=.15,
  symbolic x coords={сейчас,good,neutral,not good,poor},
  xtick=data,
  bar shift=0pt
]
\addplot coordinates {(сейчас,1) (good,8) (neutral,2) (not good,0) (poor,10)};
\end{axis}
\end{tikzpicture}
\caption{Прочность металлоизделий}
\end{figure}

\end{document}

注意添加

\pgfplotsset{compat=1.9}

  xticklabel style={
    anchor=base,
    yshift=-\baselineskip
  },

这样标签就与基线对齐了。你无论如何都应该这样做,不仅适用于 LuaLaTeX。

输出将是相同的:

在此处输入图片描述

相关内容