代码

代码

我们可以使用类似下面的方法避免 PGF 中的数学模式:\pgfmathprintnumber[assume math mode=true]

然后我可以制作出非常好看的材料,如下所示:

在此处输入图片描述

请注意,刻度数处于数学模式。我想避免这种情况。使用x tick label style={assume math mode=true}失败。

代码

\documentclass{article}
\usepackage{fontspec}% xelatex
\renewcommand\familydefault{\sfdefault}% to emphasize font change between math mode and reg mode
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{fancyvrb}% for VerbatimOut to write bar chart data to tmp file

\pgfplotsset{horizontalbarchartstyle/.style={
 % symbolic y coords=,% set dict of expected textual y coordinates, we avoid this dup of data by using "yticklabels from table" and "ytick=data"
  axis lines*=left,
  y=1cm,% vertical spacing (define the length of a unit in the y direction )
  xbar,
  bar width=5mm,% bar thickness
  y axis line style = { opacity = 0 },
  x axis line style = { black!35 },
  width=.7\textwidth,
%  xmajorgrids,
%  xminorgrids,
  xlabel={}, % optional label below x axis but useless in global style
  xmin=0,
  xmax=100,
  xtick={0,100,...,100},
%  minor xtick={12.5,37.5,...,100},
  point meta={x},
  nodes near coords={\color{blue!85!black}\pgfmathprintnumber[assume math mode=true]{\pgfplotspointmeta}\%},% puts text (set in "point meta" key) near coordinates.
  every node near coord/.style={font=\bfseries},
  nodes near coords align={horizontal},% alignment of "nodes near coords"
  enlarge y limits={abs=10mm},% add space above and below bars
  yticklabels from table={\datatable}{1}, % necessary for pgfplotstableread data
  ytick=data,
  x tick label style={font=\sffamily\footnotesize,black!35},
%  x tick style={opacity=0},
  y tick style={opacity=0},
  y tick label style={color=orange},
  legend style={font=\footnotesize},
  label style={font=\footnotesize},
  minor grid style={dotted,green!50!black},
  major grid style={dashed,orange},
  },
 }
\tikzset{addplot-color1/.style={draw=none,fill=blue!85!black}}% bar format

\newenvironment{horizontalbarchart}
  {\VerbatimOut{\jobname-horizontalbarchart.tmp}}%begin
  {\endVerbatimOut\par
  % Inject BODY into pgfplotstableread
  \pgfplotstableread[col sep=comma, header=false]{\jobname-horizontalbarchart.tmp}{\datatable}
  % Use \datatable for plot data
  \begin{tikzpicture}
   \begin{axis}[horizontalbarchartstyle]
     \addplot [addplot-color1] table [col sep=comma, y expr=\coordindex, x=0] {\datatable};
   \end{axis}
  \end{tikzpicture}\par
}%

\begin{document}

\begin{horizontalbarchart}
40,Apples
10,Strawberries
20,Rasberries
\end{horizontalbarchart}

\end{document}

答案1

assume math mode可能特定于\pgfmathprintnumber,它不是通用的 TikZ 样式。您需要更改刻度标签的打印方式,您可以使用

xticklabel={\pgfmathprintnumber[assume math mode=true]{\tick}}

注意xticklabel,不是xticklabels

xticklabel请参阅手册pgfplots第 4.15.1 节(2016/08/10 版本第 335 页)的描述。

\documentclass{article}
\usepackage{fontspec}% xelatex
\renewcommand\familydefault{\sfdefault}% to emphasize font change between math mode and reg mode
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{fancyvrb}% for VerbatimOut to write bar chart data to tmp file

\pgfplotsset{horizontalbarchartstyle/.style={
 % symbolic y coords=,% set dict of expected textual y coordinates, we avoid this dup of data by using "yticklabels from table" and "ytick=data"
  axis lines*=left,
  y=1cm,% vertical spacing (define the length of a unit in the y direction )
  xbar,
  bar width=5mm,% bar thickness
  y axis line style = { opacity = 0 },
  x axis line style = { black!35 },
  width=.7\textwidth,
%  xmajorgrids,
%  xminorgrids,
  xlabel={}, % optional label below x axis but useless in global style
  xmin=0,
  xmax=100,
  xtick={0,100,...,100},
%  minor xtick={12.5,37.5,...,100},
  point meta={x},
  nodes near coords={\color{blue!85!black}\pgfmathprintnumber[assume math mode=true]{\pgfplotspointmeta}\%},% puts text (set in "point meta" key) near coordinates.
  every node near coord/.style={font=\bfseries},
  nodes near coords align={horizontal},% alignment of "nodes near coords"
  enlarge y limits={abs=10mm},% add space above and below bars
  yticklabels from table={\datatable}{1}, % necessary for pgfplotstableread data
  ytick=data,
  xticklabel={\pgfmathprintnumber[assume math mode=true]{\tick}},
  x tick label style={font=\sffamily\footnotesize,black!35},
%  x tick style={opacity=0},
  y tick style={opacity=0},
  y tick label style={color=orange},
  legend style={font=\footnotesize},
  label style={font=\footnotesize},
  minor grid style={dotted,green!50!black},
  major grid style={dashed,orange},
  },
 }
\tikzset{addplot-color1/.style={draw=none,fill=blue!85!black}}% bar format

\newenvironment{horizontalbarchart}
  {\VerbatimOut{\jobname-horizontalbarchart.tmp}}%begin
  {\endVerbatimOut\par
  % Inject BODY into pgfplotstableread
  \pgfplotstableread[col sep=comma, header=false]{\jobname-horizontalbarchart.tmp}{\datatable}
  % Use \datatable for plot data
  \begin{tikzpicture}
   \begin{axis}[horizontalbarchartstyle]
     \addplot [addplot-color1] table [col sep=comma, y expr=\coordindex, x=0] {\datatable};
   \end{axis}
  \end{tikzpicture}\par
}%

\begin{document}

\begin{horizontalbarchart}
40,Apples
10,Strawberries
20,Rasberries
\end{horizontalbarchart}

\end{document}

相关内容