tikzscale and histograms in pgfplots

Tikzscale is giving wrong results for histograms drawn in pgfplots.

Histogram without scaling by tikzscale:

Histogram after scaling:

I have included a histogram from page 67 from pgfplots manual for version 1.7 in the MWE. The pgfplots.randn.dat file comes with the standard texlive-2012 distribution or can be downloaded from the given link.

MWE

\documentclass{report}
\usepackage{filecontents,tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.7}

% Comment to get figure without scaling
\usepackage{tikzscale}

\begin{filecontents}{figrandn.tikz}
\begin{tikzpicture}
\begin{axis}[
ybar interval,
xtick=,% reset from ybar interval
xticklabel={$[\pgfmathprintnumber\tick, \pgfmathprintnumber\nexttick)$}
]]
% a data file containing 8000 normally distributed
% random numbers of mean 0 and variance 1
\end{axis}
\end{tikzpicture}
\end{filecontents}

\begin{document}
\begin{figure}
\centering
% Comment the line below for figure without scaling.
\includegraphics[width=\textwidth,height=0.5\textwidth]{figrandn.tikz}
%Uncomment to include figure without scaling
%\input{figrandn.tikz}
\caption{Test figure}
\end{figure}
\end{document}

-
This example is not minimal. I get the same issue without the external library for example. Please always try to simplify the MWE as much as possible before posting it. This saves a lot of time for other and have the time makes you solve the problem yourself (however not here). – Martin Scharrer Jan 3 '13 at 10:50
If this is a bug in the package it should be reported directly to the package author(s). We can only give workarounds in this case and bug questions are usually "too localised (in time)". – Martin Scharrer Jan 3 '13 at 11:00
@MartinScharrer - Will do that. – devendra Jan 3 '13 at 11:00
@MartinScharrer If it turns out to be a bug then I will delete the post in couple of days. – devendra Jan 3 '13 at 11:03
@MartinScharrer This report was very helpful for me (having my package author hat on). As it was not obvious, which package caused the bug (there were similar bugs recently, which weren't tikzscale bugs), there is no real alternative for users. As each package has a separate bug tracker (if at all), there is the need of a central TeX platform (as discussing privatly via mail has different problems). Nowadays, I think tex.stackexchange is that platform. Besides, I tried to give an answer, which serves other package authors/advanced users, as well. So please do not delete this post if possible. – Patrick Häcker Jan 6 '13 at 18:28

I can confirm, that this is a bug in tikzscale. Thanks a lot for finding it. As a workaround, open tikzscale.sty, find the line containing \endlinechar=-1 and comment/delete the line.

The bug occured, because the modification of endlinechar lead to changed linebreaks when reading pgfplots.randn.dat, which lead to changed numbers (e.g., 12\n34 becomes 1234).

I do not know, what to do with this bug, yet. The modification of the endlinechar is done to avoid a slightly unprecise scaling due to leading or trailing whitespaces before or after the graphic. There are three sources of unwanted whitespace:

1. Tikzscale sometimes has a spurious whitespace, due to programming errors.
2. The external library had a spurious whitespace for some time.
3. If the user forgets to add a percent sign after \end{tikzpicture} or accidently adds a newline at the end of the tikz files, this is spurious whitespace, too.

So much to the problems, now let's have a look at the solutions:

1. Can always be fixed by a new version of tikzscale, so this should not be a big deal.
2. Can be fixed by using a recent development version of TikZ, which is unfortunate for the user, but possibly recommended anyway due to other bug fixes.
3. Is the hardest part, because the user has to always remember that problem when creating TikZ files, which is a poor user experience. I guess I have to modify \begin{tikzpicture} and \end{tikzpicture} so that the original endlinechar is restored inside of the environment.

Update With (the just uploaded) tikzscale version 0.2.1, the problem is fixed.

-