# Image shifts when removing percent (%) signs with standalone class

I have occasionally observed that some LaTeX macros behave differently with and without end-of-line comments. My understanding of this is that a macro may leave a space on the stack that for some reason is removed if the macro is ended with a percent sign. I have never quite understood why this happens so I've slowly fallen into the habit of ending all "function calls" in LaTeX with %. In the present case, I discovered that \pstScalePoints and \newpsstyle behaves just like that and took the effort to isolate the problem into the following MWE:

\documentclass{standalone}
\usepackage{pstricks}
\usepackage{pstricks-add}
\usepackage{pst-plot}
\listfiles
\begin{document}
\readdata{\mytable}{table.txt}
\pstScalePoints(1.0,1.0){1.0 div}{1.0 div}%  <-- NB!
\newpsstyle{legendstyle}{fillstyle=solid}%   <-- NB!
\begin{psgraph}(0,0)(0,0)(1,1){1cm}{1cm}
\listplot{\mytable}
\end{psgraph}
\end{document}


Problem: The output changes if the two end-of-line % signs are removed. My suspicion is that the macros leave behind a space on the stack, but I do not know how to correct it - other than stubbornly adding % to all the macro endings!

The contents of file table.txt is:

0.0, 0.00
0.2, 0.04
0.4, 0.16
0.6, 0.36
0.8, 0.64
1.0, 1.00


I halfway expect that the problem depends on which packages I am using so I therefore include the output from \listfiles:

pstricks.sty      2013/12/12 v0.60
pstricks.tex      2014/10/25 v2.60
pst-xkey.tex      2005/11/25 v1.6
pst-fp.tex        2014/10/25 v2.60
pstricks-add.sty  2010/02/11 v.0.14
pst-plot.sty      2011/04/13
pst-xkey.sty      2005/11/25 v1.6
pst-plot.tex      2014/08/23 1.70
pst-node.sty      2010/04/22
pst-node.tex      2014/08/04 1.35
pstricks-add.tex  2014/12/08 v3.77
pstricks-add.cfg  2005/01/10 v0.1


Finally, I've attached two pictures illustrating the problem:

1. With end-of-line comment

(source: ntnu.no)

2. Without end-of-line comment

(source: ntnu.no)

• The shift that you observe are the two spaces corresponding to the two line ends. You should rather ask why sometimes/often the space at the line end does not become visible. There are two major reasons why the space is ignored: If the line ends with a macro name like \abc, then the space is consumed as part of the parsing rules of TeX. The other situation is that the command explicitly consumes the space, like \begin or \end statements do, and apparently also \readdata does. You can achieve the same effect for your own macro definitions by ending them with \ignorespaces. – gernot Jan 1 '17 at 17:26
• By default, standalone typesets its contents in a horizontal box, so spaces are significant. You need also a % after \readdata{\mytable}{table.txt}. In a normal document those spaces would be ignored since LaTeX has not yet started a paragraph. – egreg Jan 1 '17 at 17:46
• your first line implies that the % behaviour is related to macros which is not the case. compare a<newline>b which typesets as a b but if you comment out the newline then naturally it comments out the space so you get ab – David Carlisle Jan 1 '17 at 21:24
• Thanks @egreg, for enlightening me about standalone. I did not come to my mind that it works in horisontal mode! On the other hand, I cannot see any difference if \readdata is ended with %or not. Is there something I have (still) misunderstood? – Tore H-W Jan 1 '17 at 22:50
• @ToreH-W Actually, \readdata calls an inner macro whose replacement text ends with \ignorespaces, so indeed it does not matter whether you have % after that line. Just checked. I was bitten by the issue a couple of times, so I learnt about it. – egreg Jan 1 '17 at 22:56

## 1 Answer

This is a specific problem of standalone. You can look at What is the use of percent signs (%) at the end of lines? for understanding what follows.

By default, standalone typesets the text in the document environment in a horizontal box (\mbox, essentially), for later cropping.

In a horizontal box, spaces are significant, so the end-of-lines must be hidden with % lest they're converted into spaces.

In a normal document, say with \documentclass article, you wouldn't see those spaces, because they are seen when TeX has not yet started a paragraph.

So

\documentclass{standalone}
\usepackage{pstricks}
\usepackage{pstricks-add}
\usepackage{pst-plot}
\begin{document}
\readdata{\mytable}{table.txt}%  <-- NB!
\pstScalePoints(1.0,1.0){1.0 div}{1.0 div}%  <-- NB!
\newpsstyle{legendstyle}{fillstyle=solid}%   <-- NB!
\begin{psgraph}(0,0)(0,0)(1,1){1cm}{1cm}
\listplot{\mytable}
\end{psgraph}
\end{document}


On the other hand,

\documentclass{article}
\usepackage{pstricks}
\usepackage{pstricks-add}
\usepackage{pst-plot}
\begin{document}

\readdata{\mytable}{table.txt}
\pstScalePoints(1.0,1.0){1.0 div}{1.0 div}
\newpsstyle{legendstyle}{fillstyle=solid}

\begin{psgraph}(0,0)(0,0)(1,1){1cm}{1cm}
\listplot{\mytable}
\end{psgraph}

\end{document}


needs no % because the paragraph starts with \begin{psgraph}.

If you find yourself doing many of this % additions, you can consider adding something to your document:

\documentclass[varwidth]{standalone}
\usepackage{pstricks}
\usepackage{pstricks-add}
\usepackage{pst-plot}

\newcommand{\startsetup}{%
\chardef\oldendlinechar\endlinechar
\endlinechar=-1
}
\newcommand{\stopsetup}{\endlinechar=\oldendlinechar}

\begin{document}

\startsetup
\readdata{\mytable}{table.txt}
\pstScalePoints(1.0,1.0){1.0 div}{1.0 div}
\newpsstyle{legendstyle}{fillstyle=solid}
\stopsetup

\begin{psgraph}(0,0)(0,0)(1,1){1cm}{1cm}
\listplot{\mytable}
\end{psgraph}
\end{document}
`