bchart、tikzpicture 等中的小数逗号

bchart、tikzpicture 等中的小数逗号

有没有办法在 bchart、tikzpicture 等环境中重新定义小数分隔符?我需要排版小数点逗号,而不是小数点。

最小示例:

\documentclass{article}
\usepackage{bchart}

\begin{document}
    \begin{bchart}[max=10]
        \bcbar[text=xxx]{5.5}
        \bcskip{4pt}
        \bcbar[text=yyy]{6.75}
    \end{bchart}
\end{document}

编辑

\documentclass{article}
\usepackage{pgf-pie}
\begin{document}
\begin{tikzpicture}
    \pie{20.1/xxx,79.9/yyy}
\end{tikzpicture}
\end{document}

答案1

bchart包不太灵活,不幸的是,甚至不使用pgfkeys。但您可以通过创建自己的\Bcbar宏来使用 percusse 使用 PGFmath 的数字打印的想法:

\newcommand*\Bcbar[2][]
  {\bcbar[{value={\pgfmathprintnumber[my value style/.try]{#2}}, #1}]{#2}}
\pgfset{number format/my value style/.style={use comma}}

这将使用value覆盖\bcbarvalue宏的键(参见David的回答)。

但是,这意味着您需要使用宏\Bcbar而不是\bcbar宏:

\documentclass{article}
\usepackage{bchart}
\newcommand*\Bcbar[2][]
  {\bcbar[{value={\pgfmathprintnumber[my value style/.try]{#2}}, #1}]{#2}}
\pgfset{number format/my value style/.style={use comma}}
\begin{document}
\begin{bchart}[max=10]
  \Bcbar[text=xxx]{5.5}
  \bcskip{4pt}
  \Bcbar[text=yyy]{6.75}
\end{bchart}
\end{document}

由于bchart环境每次都会重新定义\bcbar,这可能是工作量/线路最少的解决方案。


另一个想法是重新定义value密钥并每次使用它:

\documentclass{article}
\usepackage{bchart}
\makeatletter
\define@key{bcbar}{value}
  {\renewcommand*\bcbarvalue{\pgfmathprintnumber[my value style/.try]{#1}}}
\makeatother
\pgfset{number format/my value style/.style={use comma, read comma as period}}
\begin{document}
\begin{bchart}[max=10]
  \bcbar[text=xxx, value=5.5]{5.5}
  \bcskip{4pt}
  \bcbar[text=yyy, value={6,75}]{6.75}
\end{bchart}
\end{document}

当然,您可以将这两种解决方案结合起来,这样您就可以使用一个使用\pgfmathprintnumber设施的值键,而另一个text value不使用 设施的键,这样您仍然可以编写任何文本。

答案2

在此处输入图片描述

我只是在一个宏中更改了一行,但不幸的是,这几乎是整个包。

\documentclass{article}
\usepackage{bchart}

\makeatletter
\renewenvironment{bchart}[1][]{%
  % Bars:
  \newcommand{\bcbar}[2][]{
    % Set defaults:
    \renewcommand{\bcbarcolor}{blue!20}
    \renewcommand{\bcbartext}{}
    \renewcommand{\bcbarlabel}{}
    \renewcommand{\bcbarvalue}{$\mathcode`\.=`\,##2$\bcunit}%<<<<<<<<<<<<<<<<<<<<<
    \renewcommand{\bcplainbar}{false}
    % Read parameters:
    \setkeys{bcbar}{##1}
    % Draw bar:
    \fill[color=\bcbarcolor,fill,draw] (0,\bcpos) rectangle ($##2-\bcmin*(\bcwidth/\bcrange,0) + (0,\bcpos-5mm)$);
    \draw (0,\bcpos) rectangle ($##2-\bcmin*(\bcwidth/\bcrange,0) + (0,\bcpos-5mm)$);
    \ifthenelse{\equal{\bcplainbar}{true}}{}{
      % Write value:
      \node[anchor=west] at ($##2-\bcmin*(\bcwidth/\bcrange,0) + (0,\bcpos-2.5mm)$) {\bcfontstyle\bcbarvalue};
    }
    % Write text:
    \node[anchor=west] at (0,\bcpos-2.5mm) {\bcfontstyle\bcbartext};
    % Write label:
    \node[anchor=east] at (0,\bcpos-2.5mm) {\bcfontstyle\bcbarlabel};
    % Move vertical position downward:
    \addtolength{\bcpos}{-5mm}
  }%
  % Labels:
  \newcommand{\bclabel}[1]{
    % Write label:
    \node[anchor=east] at (0,\bcpos) {\bcfontstyle##1};
  }%
  % General skips:
  \newcommand{\bcskip}[2][]{
    % Set defaults:
    \renewcommand{\bcskiplabel}{}
    % Read parameters:
    \setkeys{bcskip}{##1}
    % Write label:
    \node[anchor=east] at ($(0,\bcpos) - 0.5*(0,##2)$) {\bcfontstyle\bcskiplabel};
    % Move vertical position downward:
    \addtolength{\bcpos}{-##2}
  }%
  % Small skips:
  \renewcommand{\smallskip}[1][]{\bcskip[##1]{2.5mm}}%
  % Medium skips:
  \renewcommand{\medskip}[1][]{\bcskip[##1]{5.0mm}}%
  % Large skips:
  \renewcommand{\bigskip}[1][]{\bcskip[##1]{7.5mm}}%
  % X-axis label:
  \newcommand{\getbcxlabel}{}%
  \newcommand{\bcxlabel}[1]{
    \renewcommand{\getbcxlabel}{##1}
  }%
  \newcommand{\bcrange}{\bcstripunit{\dimexpr\bcmax pt-\bcmin pt\relax}}
  % Set defaults:
  \renewcommand{\bcunit}{}%
  \renewcommand{\bcmin}{0}%
  \renewcommand{\bcmax}{100}%
  \renewcommand{\bcstep}{\bcrange}%
  \renewcommand{\bcsteps}{0,\bcstep,...,\bcrange}%
  \renewcommand{\bcscale}{1}%
  \renewcommand{\bcplainchart}{false}%
  \setlength{\bcpos}{-2.5mm}%
  \setlength{\bcwidth}{8cm}%
  % Read parameters:
  \setkeys{bchart}{#1}%
  % Draw chart:
  \begin{tikzpicture}[scale=\bcscale]
}{
    % Draw axes:
    \addtolength{\bcpos}{-2.5mm}
    \draw (0,\bcpos) -- (\bcwidth,\bcpos);
    \draw (0,0) -- (0,\bcpos);
    % Draw scale:
    \ifthenelse{\equal{\bcplainchart}{true}}
    { % Scale off
      % Set position for x-axis label:
      \coordinate (labelpos) at (0,\bcpos-2mm);
    }{ % Scale on
      % Draw start value and set position for x-axis label:
      \draw (0,\bcpos) -- (0,\bcpos-1mm);
      \node[anchor=north] (n) at (0,\bcpos-1mm)
        {\bcfontstyle\bcstripunit{\dimexpr\bcmin pt\relax}\bcunit};
      \coordinate (labelpos) at (n.south);
      % Draw other values:
      \foreach \x in \bcsteps {
        \ifthenelse{\equal{\x}{0}}{}{
          \draw ($\x*(\bcwidth/\bcrange,0) + (0,\bcpos)$) -- ($\x*(\bcwidth/\bcrange,0) + (0,\bcpos-1mm)$);
          \node[anchor=north] at ($\x*(\bcwidth/\bcrange,0) + (0,\bcpos-1mm)$)
            {\bcfontstyle\bcstripunit{\dimexpr\bcmin pt+\x pt\relax}\bcunit};
        }
      }
    }
    % Write x-axis label:
    \ifthenelse{\equal{\getbcxlabel}{}}{}{
      \node[anchor=north,inner sep=0.5mm] at ($0.5*(\bcwidth,0) + (labelpos)$) {\bcfontstyle\getbcxlabel};
    }
  \end{tikzpicture}%
}
\makeatother

\begin{document}

    \begin{bchart}[max=10]
        \bcbar[text=xxx]{5.5}
        \bcskip{4pt}
        \bcbar[text=yyy]{6.75}
    \end{bchart}

\end{document}

答案3

当然你也可以直接使用pgfplots(进行一些调整以匹配视觉输出):

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xbar, 
xmin=0,xmax=10, 
symbolic y coords={xxx,yyy},
ytick=data,
nodes near coords, nodes near coords align={horizontal},
axis lines = left, xtick={0,10},axis line style={-},
enlarge y limits=.5,height=3.5cm,width=8cm,
xtick align=outside,
ytick style={draw=none},
yticklabel style={anchor=west,xshift=10},
axis on top,
/pgf/number format/use comma,
]
\addplot coordinates {(5.5,xxx) (6.75,yyy)};
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容