Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to set papersize based on PSTricks settings. The PSTricks settings may be a value without dimension and/or a value with dimension.

Honestly I have not read the TeXBook because of my laziness. I just read some examples from others but I don't know the hidden theory. For short, I have two questions as follows.

  1. why does the following calculation not work? How should I do arithmetic for values with dimension and without dimension.
  2. what is the best practice to reduce the rounding error for complicated length and dimensionless value calculation?
\documentclass{minimal}
\usepackage{pstricks}
\psset
{
    xunit=2cm,
    yunit=1cm
}

\newcommand\Left{-4}
\newcommand\Right{4}
\newcommand\Bottom{-4}
\newcommand\Top{4}
\newcommand\Padding{0}


\topmargin=-72.27pt
\oddsidemargin=-72.27pt
\parindent=0pt
\paperwidth=\dimexpr\Right\psxunit-\Left\psxunit+2\Padding\psxunit\relax
\paperheight=\dimexpr\Top\psyunit-\Bottom\psyunit+2\Padding\psyunit\relax
\special{papersize=\the\paperwidth,\the\paperheight}

\begin{document}
\begin{pspicture}(\Left,\Bottom)(\Right,\Top)
\psframe(\Left,\Bottom)(\Right,\Top)
\end{pspicture}
\end{document}
share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

Because egreg's suggestion using \numexpr cannot be used for non-integer values, I have to use my own as follows. It works but it seems to be too complicated and needs further simplification.

\documentclass{minimal}
\usepackage{pstricks}
\psset
{
    xunit=1cm,
    yunit=1cm
}

\newcommand\Left{-1}
\newcommand\Right{1}
\newcommand\Bottom{-1}
\newcommand\Top{1}
\newcommand\Padding{0.35}

\topmargin=\dimexpr\Padding\psyunit-72.27pt\relax
\oddsidemargin=\dimexpr\Padding\psxunit-72.27pt\relax
\parindent=0pt
\paperwidth=\dimexpr\Right\psxunit-\Left\psxunit+2\dimexpr\Padding\psxunit\relax\relax
\paperheight=\dimexpr\Top\psyunit-\Bottom\psyunit+2\dimexpr\Padding\psyunit\relax\relax
\special{papersize=\the\paperwidth,\the\paperheight}

\begin{document}
\begin{pspicture}[showgrid](\Left,\Bottom)(\Right,\Top)
\psframe(\Left,\Bottom)(\Right,\Top)
\end{pspicture}
\end{document}
share|improve this answer
add comment

You are actually doing \psxunit times 4-(-4)+20 = 28.

\paperwidth=\dimexpr\psxunit*\numexpr(\Right-\Left+2*\Padding)\relax

The 2\Padding bit expands to "20".

share|improve this answer
    
+1 for the first answer, how about the second question? (I will revisit this later and give a green check mark) –  xport Jul 22 '11 at 11:23
    
As usual, the calculations are done in terms of scaled points, so the difference is less than the wavelength of visible light and will influence pixels only in very unfortunate circumstances. In this particular case I get \paperwidth=29834872sp both with the \dimexpr method and with the traditional \advance method. –  egreg Jul 22 '11 at 11:33
    
If I use 4.5 instead of 4, the compilation fails. How to fix it? –  xport Jul 22 '11 at 12:38
    
Inside \numexpr only integers are allowed. Just use integers and a sufficiently small base dimension. –  egreg Jul 22 '11 at 12:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.