A slightly different approach then percusse’s:
We use PGF math’s \pgfmathprintnumber
macro with
- the option
use comma
and
- the option
zero-width sign
which uses mathtools
’ \mathllap
macro that is \robustify
-ed by etoolbox
. You can also use \llap
instead (see comment in code).
Two special macros from the pgfmathprintnumber
needs to adjusted for this. (I have ignored the show pos
option here that forces a plus sign.)
A rather unorthodox but safe approach would be to use \x\ifdim\x pt<0pt\hphantom{-}\fi
, i.e. checking if \x
is less then 0
(works only if \x
is already an evaluated, expandable value) and add a hidden -
after the number, thus centering the sign-less part of the number.
Both solutions have the disadvantage that the node’s border do not correlate to the actual text. (But if you use neither draw
nor the border on the west and the right side, this shouldn’t be a problem.)
I also included the node from the second loop in the first loop, so that one doesn’t have to fill
the node, this makes the drawing independent from the background (the just may be a line or a shading going on). This is usually also doable with the markings
decoration and the mark connection node
options, though that has failed me today.
I also included text depth=+0pt
in the node options with sets the depth of the node text box to zero hiding the depth of the ,
and of -
which would result in slightly different vertical alignment of the nodes because we have three different types of input:
- (zero-)positive integer (neither
,
nor -
)
- negative integer (only
-
)
- positive and negative floats (depth of
,
is greater than the depth of -
).
Another solution would be to use the base
anchor to place the nodes instead of the (default) center
one.
Code
\documentclass[12pt,tikz,convert=false]{standalone}
\begin{filecontents}{B6.dat}
-1 0
0 5
1 0
\end{filecontents}
\usepackage{etoolbox,mathtools}
\robustify\mathllap
\makeatletter
\newif\ifpgfmathprintnumber@zerowidth@sign
\def\pgfmathprintnumber@zerowidth@#1{\mathllap{#1}}% or \llap{$#1$} without mathtools/etoolbox
\pgfqkeys{/pgf/number format}{zero-width sign/.is if=pgfmathprintnumber@zerowidth@sign,zero-width sign/.default=true}
\def\pgfmathprintnumber@fixed@styleDEFAULT@impl@noperiod@printsign#1{%
\def\pgfmathfloat@loc@TMPb{#1}%
\edef\pgf@tempa{\ifpgfmathprintnumber@zerowidth@sign\pgfmathprintnumber@zerowidth@{#1}\else#1\fi}%
\ifx\pgfmathfloat@loc@TMPb\pgfmathfloatparsenumber@tok@MINUS
\expandafter\expandafter\expandafter\def\expandafter\expandafter\expandafter\pgfmathresult\expandafter\expandafter\expandafter{\expandafter\pgfmathresult\pgf@tempa}%
\let\pgfmathfloat@loc@TMPb=\pgfutil@empty
\else
\ifx\pgfmathfloat@loc@TMPb\pgfmathfloatparsenumber@tok@PLUS
\expandafter\expandafter\expandafter\def\expandafter\expandafter\expandafter\pgfmathresult\expandafter\expandafter\expandafter{\expandafter\pgfmathresult\pgf@tempa}%
\let\pgfmathfloat@loc@TMPb=\pgfutil@empty
\else
\ifpgfmathprintnumber@showpositive
\expandafter\def\expandafter\pgfmathresult\expandafter{\pgfmathresult +}%
\fi
\fi
\fi
\ifnum\c@pgf@counta>0
\def\pgfmathprintnumber@fixed@styleDEFAULT@impl@noperiod@NEXT{%
\expandafter\pgfmathprintnumber@fixed@styleDEFAULT@impl@noperiod@printtrailingdigits\pgfmathfloat@loc@TMPb
}%
\else
\def\pgfmathprintnumber@fixed@styleDEFAULT@impl@noperiod@NEXT{%
\expandafter\pgfmathprintnumber@fixed@styleDEFAULT@impl@noperiod@counteverythird\pgfmathfloat@loc@TMPb
}%
\fi
\pgfmathprintnumber@fixed@styleDEFAULT@impl@noperiod@NEXT
}
\def\pgfmathprintnumber@fixed@styleDEFAULT@impl@noperiod@printall#1{%
\def\pgfmathfloat@loc@TMPb{#1}%
\let\pgfmathfloat@loc@TMPc=\pgfutil@empty
\ifx\pgfmathfloat@loc@TMPb\pgfmathfloatparsenumber@tok@MINUS
\ifpgfmathprintnumber@zerowidth@sign
\let\pgfmathfloat@loc@TMPc\pgfmathprintnumber@zerowidth@
\fi
\else
\ifx\pgfmathfloat@loc@TMPb\pgfmathfloatparsenumber@tok@PLUS
\ifpgfmathprintnumber@zerowidth@sign
\let\pgfmathfloat@loc@TMPc\pgfmathprintnumber@zerowidth@
\fi
\else
\ifpgfmathprintnumber@showpositive
\def\pgfmathfloat@loc@TMPc{+}%
\fi
\fi
\fi
\expandafter\pgfmathprintnumber@fixed@styleDEFAULT@impl@noperiod@printall@\pgfmathfloat@loc@TMPc#1%
}
\makeatother
\definecolor{rouge}{RGB}{233,55,77}
\def\xmin{-1.1}\def\xmax{1.1}\def\ymin{-.6}\def\ymax{5.6}
\begin{document}
\footnotesize
\begin{tikzpicture}[>=latex,scale=1.4,x=4cm,y=1cm]
%%%% background
\fill[rouge!10] (\xmin,\ymin) rectangle (\xmax,\ymax);
%%%% grid and labels
\begin{scope}
\clip (\xmin,\ymin) rectangle (\xmax,\ymax);
\foreach \x in {-1,-0.75,...,1}
\path[rouge!35]
node[inner sep=2pt,anchor=south,font=\tiny\everymath{\color{rouge}\mathsf},text depth=+0pt] at (\x,-.5) {\pgfmathprintnumber[zero-width sign,use comma]{\x}}
edge (\x,\ymin)
edge (\x,\ymax);
\end{scope}
%%%% curve
\draw plot file{B6.dat};
%%%% external frame
\draw[line width=.6pt,rouge!50] (\xmin,\ymin) rectangle (\xmax,\ymax);
\end{tikzpicture}
\end{document}
Output
