3

Consider the following example, which produces the same figure as here, but the code is generalized.

Code

% pdflatex -shell-escape test.tex

\documentclass{article}

\usepackage{auto-pst-pdf,pst-node}
\usepackage{siunitx}

\def\path[#1]{%
  \pscustom[dimen=middle,#1]{
    \psarc(!\laengde \radius sub \radius){\radius}{-90}{90}
    \psarc(!\laengde \radius sub 3 \radius mul){\radius}{-90}{90}
    \psframe(\laengde,\bredde)
  }\ignorespaces
}

\begin{document}

\begin{figure}
\def\laengde{216 }
\def\bredde{120 }
\edef\maksL{\the\numexpr\laengde+20\relax\space}
\edef\maksB{\the\numexpr\bredde+20\relax\space}
\edef\radius{\the\numexpr\bredde/4\relax\space} % Needs to be changed.
 \centering
 \psset{
   unit=0.05
 }
  \begin{pspicture}(\maksL,\maksB)
   \psclip{\path[linestyle=none]}
     \psframe[
       fillstyle=vlines,
       hatchcolor=red,
       hatchsep=1pt,
       linestyle=none
     ](!\laengde \radius sub 0)(\laengde,\bredde)
   \endpsclip
    \path[]
   \psdots(!\laengde \radius sub \radius)(!\laengde \radius sub 3 \radius mul)
  \psset{
    linestyle=dotted,
    offset=12pt,
    arrows=|<*->|*,
    nrot=:U
  }
   \pcline(0,\bredde)(\laengde,\bredde)
   \ncput*{\SI{\laengde}{\m}}
   \pcline(\laengde,\bredde)(\laengde,0)
   \ncput*{\SI{\bredde}{\m}}
  \end{pspicture}
\end{figure}

\end{document}

Output

Output.

Question

I have defined \radius as

\def\radius{\the\numexpr\bredde/4\relax\space}

but this only works correctly if \bredde is a multiple of 4. I have tried with both \dimexpr and \newlength but I can't make it work (probably do to some trivial mistake).

8
  • \numexpr won't perform floating point division. You could try with the fp package. May 1, 2013 at 1:36
  • @GonzaloMedina Okay. If I can make you show my how to do it, it would be nice. (I have never used the package before.) May 1, 2013 at 1:38
  • In the preamble, \usepackage{fp} and then, instead of \def\radius{\the\numexpr\bredde/4\relax\space}, use \FPeval{radius}{\bredde/4} (I haven't tested, though, to see if this solves your problem). May 1, 2013 at 1:40
  • @GonzaloMedina If I do that, I get an error. (I'll update my answer to show the code.) May 1, 2013 at 1:43
  • Can we discuss this in chat? Adding code in comments is not really possible. May 1, 2013 at 1:49

2 Answers 2

2
\documentclass{article}
\usepackage{auto-pst-pdf,pst-node}
\usepackage{siunitx}

\def\path[#1]{%
  \pscustom[dimen=middle,#1]{%
    \psarc(\dimexpr\Laengde-\Radius,\Radius){\Radius}{-90}{90}
    \psarc(\dimexpr\Laengde-\Radius,3\Radius){\Radius}{-90}{90}
    \psframe(\Laengde,\Bredde)}}
\begin{document}

\psset{unit=0.05}
\def\laengde{216} \def\bredde{120} 
\newlength\Laengde \Laengde=\laengde\psunit
\newlength\Bredde  \Bredde=\bredde\psunit
\newlength\Radius  \Radius=0.25\Bredde
\begin{pspicture}(\dimexpr\Laengde+5mm,\dimexpr\Bredde+5mm)
 \psclip{\path[linestyle=none]}
   \psframe[fillstyle=vlines,hatchcolor=red,hatchsep=1pt,linestyle=none]%
     (\dimexpr\Laengde-\Radius,0)(\Laengde,\Bredde)
 \endpsclip
 \path[]
 \psdots(\dimexpr\Laengde-\Radius,\Radius)(\dimexpr\Laengde-\Radius,3\Radius)
 \psset{linestyle=dotted,offset=12pt,arrows=|<*->|*,nrot=:U}
 \pcline(0,\Bredde)(\Laengde,\Bredde)
 \ncput*{\SI{\laengde}{\m}}
 \pcline(\Laengde,\Bredde)(\Laengde,0)
 \ncput*{\SI{\bredde}{\m}}
\end{pspicture}

\end{document}

you are very generous with your line ends. It is not a good idea in general to write

\psset{
  foo= ... ,
  bar= ... 
}

If the macros do not delete the trailing spaces then you will have some of it in your document. If you want to have it in several lines then use:

\psset{%
  foo= ... ,
  bar= ... %
}

Same for other macros.

4
  • Thank you for the code. I'll try to avoid the usage of a new line every time I call a package option (and other things). May 1, 2013 at 7:37
  • @SvendTveskæg: New lines for each package option and psset invoked in the preamble will not contribute trailing white spaces in the output. May 1, 2013 at 8:06
  • @Bugbusters: you should read my comments carefully because giving such statements!
    – user2478
    May 1, 2013 at 8:46
  • My comment was given in response to the first comment by Svend Tveskaeg. It has nothing to do with your comment and I did not see any comment from you. Where is your comment? :-) May 1, 2013 at 9:19
1

Using my package below,

% ========================================================
% Copyright (c) 2013 Bugbusters. All rights reservered.
% LPPL LaTeX Public Project License
% ========================================================
\NeedsTeXFormat{LaTeX2e}[1994/06/01] 
\ProvidesPackage{pst-sex}[2013/04/27 v3.141592654 Constant declator for StackEXchange]
\RequirePackage[nomessages]{fp}
\RequirePackage{pstricks}

% Removing a trailing space in the original fp.sty.
% I think this problem should be fixed directly to the fp.sty
% to make this package more compact.
\def\FP@pow#1#2#3{%
    % #1 macro, which gets the result
    % #2 base
    % #3 exponent
    %
    \FP@beginmessage{POW}%
    %
    {\def\FP@beginmessage##1{}%
     \def\FP@endmessage##1{}%
     %
     \FPifzero{#2}%
        \FP@pow@zero{#3}%
     \else%
        \FPln\FP@tmpd{#2}%
        \FPmul\FP@tmpd\FP@tmpd{#3}%
        \FPexp\FP@tmp\FP@tmpd%
     \fi%
     %
     \global\let\FP@tmp\FP@tmp%
    }%
    %
    \FP@endmessage{}%
    %
    \let#1\FP@tmp%
}


\def\filter#1,#2#3\relax#4{%
    \if t#1%
        trunc(#4:#2)%
    \else   \if r#1%
        round(#4:#2)%
    \else   \if c#1%
        clip(#4)%
    \else
        \ERRORHERE
    \fi\fi\fi}

% \pstVerb concatenator
\def\LoadConstants{}

% Constant declarator invoked in the preamble
\newcommand\const[3][]{%
    \if\relax\detokenize{#1}\relax
        \edef\temporary{#3}%
    \else
        \edef\temporary{\filter#1,any\relax{#3}}%
    \fi
    \expandafter\FPeval\csname#2\expandafter\endcsname
        \expandafter{\expandafter{\temporary}}%
    \toks0=\expandafter{\LoadConstants\pstVerb}
    \edef\LoadConstants{\the\toks0 {/#2 \csname#2\endcsname\space def}}}


\def\pst@@picture@i[#1]#2(#3,#4){%
% BEGIN - redefine \const for invocation inside pspicture
    \renewcommand\const[3][]{%
    \if\relax\detokenize{##1}\relax
        \edef\temporary{##3}%
    \else
        \edef\temporary{\filter##1,any\relax{##3}}%
    \fi
    \expandafter\FPeval\csname##2\expandafter\endcsname
        \expandafter{\expandafter{\temporary}}%
        \pstVerb{/##2 \csname##2\endcsname\space def}\ignorespaces}%
% END - redefine \const for invocation inside pspicture
    \@ifnextchar(% ignore anything between [] and ()
% BEGIN - inject \LoadConstants into pspicture
    {\pst@@@picture[#1](#3,#4)\LoadConstants}%
    {\pst@@@picture[#1](0,0)(#3,#4)\LoadConstants}%
% END - inject \LoadConstants into pspicture    
}

\endinput
% pst-sex.sty

your code can be simplified as follows.

\documentclass[pstricks]{standalone}
\usepackage{pst-sex}
\usepackage{pst-node}
\usepackage{siunitx}

\const{laengde}{216}
\const{bredde}{120}

\const{maksL}{laengde+20}
\const{maksB}{bredde+20}
\const{radius}{bredde/4} % Needs to be changed.
\psset{unit=0.05}

\def\path[#1]{%
  \pscustom[dimen=middle,#1]{
    \psarc(!laengde radius sub radius){!radius}{-90}{90}
    \psarc(!laengde radius sub 3 radius mul){!radius}{-90}{90}
    \psframe(!laengde bredde)
  }\ignorespaces
}


\begin{document}
\begin{pspicture}(\maksL,\maksB)
    \psclip{\path[linestyle=none]}
        \psframe[fillstyle=vlines,hatchcolor=red,hatchsep=1pt,linestyle=none](!laengde radius sub 0)(!laengde bredde)
    \endpsclip
    \path[]
    \psdots(!laengde radius sub radius)(!laengde radius sub 3 radius mul)
    \psset{linestyle=dotted,offset=12pt,arrows=|<*->|*,nrot=:U}
    \pcline(!0 bredde)(!laengde bredde)
    \ncput*{\SI{\laengde}{\m}}
    \pcline(!laengde bredde)(!laengde 0)
    \ncput*{\SI{\bredde}{\m}}
\end{pspicture}
\end{document}

Edit:

No problem if you write each option in a separate line as follows as long as it is defined in the preamble.

\usepackage
[
a4paper,
hmargin=1cm,
vmargin=2cm,% excessive commas are always ignored
]{geometry}


\psset
{
linewidth=2pt,
linecolor=red,
fillstyle=solid,
}
1
  • Nice! I'll start using your package if it gets put on CTAN. :) May 1, 2013 at 7:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .