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.

Sometimes it will be convenient to work in big integers for the coordinates. To control the physical size, we can adjust the unit for scaling.

Unfortunately, changing unit to a small size will make the grid, that is very useful in navigation, become very dense and no longer useful.

I have attempted to reduce the label size but it is still dense.

\documentclass[pstricks,border=12pt]{standalone}
\psset{unit=1mm}
\addtopsstyle{gridstyle}
{
    gridlabels=3pt,
}
\begin{document}
\begin{pspicture}[showgrid](50,50)

\end{pspicture}
\end{document}

enter image description here

Ideally we should have a control to change the step of the grid lines and labels. For example, the grid lines and labels are made and put only for the multiple of 5. How to do it? Is it possible?

Note

Don't suggest me to use \psaxes because I don't want to load pst-plot whenever I need the above mentioned requirement.

share|improve this question
2  
A new key, for example gridstep, is really needed. :-) –  Fifa Earth Cup 2014 Oct 6 '13 at 8:14
add comment

1 Answer

up vote 6 down vote accepted
+1000

The following is a very elementary adaptation of the current grid feature (and has not been tested extensively). It extends it to add gridstep as a key-value (default is 1).

enter image description here

\documentclass[pstricks,border=12pt]{standalone}
\makeatletter
\define@key[psset]{pstricks}{gridstep}[1]{\def\psk@gridstep{/gridstep #1 def }}
\psset{gridstep=1}% Default grid step

\addto@pscode{
/StepGrid {  
  newpath 
  /a 4 string def 
  /b ED %               psk@gridlabels in pt
  /c ED %               { \pst@usecolor\psgridlabelcolor }
  /n ED %               psk@griddots
  cvi dup 1 lt { pop 1 } if 
  /s ED %               \psk@subgriddiv
  s div dup 0 eq { pop 1 } if 
  /dy ED s div dup 0 eq { pop 1 } if %  \pst@number\psyunit abs
  /dx ED dy div round dy mul %      \pst@number\psxunit abs
  /y0 ED dx div round dx mul 
  /x0 ED dy div round cvi 
  /y2 ED dx div round cvi 
  /x2 ED dy div round cvi 
  /y1 ED dx div round cvi 
  /x1 ED 
  /h y2 y1 sub 0 gt { 1 } { -1 } ifelse def 
  /w x2 x1 sub 0 gt { 1 } { -1 } ifelse def 
  b 0 gt { 
    /z1 b 4 div CLW 2 div add def
%    /Helvetica findfont b scalefont setfont 
    /b b .95 mul CLW 2 div add def } if 
  systemdict /setstrokeadjust known 
    { true setstrokeadjust /t { } def }
    { /t { transform 0.25 sub round 0.25 add exch 0.25 sub round 0.25 add
       exch itransform } bind def } ifelse 
  gsave n 0 gt { 1 setlinecap [ 0 dy n div ] dy n div 2 div setdash } { 2 setlinecap } ifelse 
  /i x1 def 
  /f y1 dy mul n 0 gt { dy n div 2 div h mul sub } if def 
  /g y2 dy mul n 0 gt { dy n div 2 div h mul add } if def 
  x2 x1 sub w mul 1 add dup 1000 gt { pop 1000 } if 
  { i dx mul dup y0 moveto 
    b 0 gt 
      { gsave c i a cvs dup stringwidth pop 
        /z2 ED w 0 gt {z1} {z1 z2 add neg} ifelse 
    h 0 gt {b neg}{z1} ifelse 
        rmoveto %
        i gridstep mod 0 eq { show } { pop } ifelse %show 
        grestore } if 
    dup t f moveto 
    i gridstep mod 0 eq { g t L stroke } if %g t L stroke 
    /i i w add def 
  } repeat 
  grestore 
  gsave 
  n 0 gt
  % DG/SR modification begin - Nov. 7, 1997 - Patch 1
  %{ 1 setlinecap [ 0 dx n div ] dy n div 2 div setdash }
    { 1 setlinecap [ 0 dx n div ] dx n div 2 div setdash }
  % DG/SR modification end
    { 2 setlinecap } ifelse 
  /i y1 def 
  /f x1 dx mul n 0 gt { dx n div 2 div w mul sub } if def 
  /g x2 dx mul n 0 gt { dx n div 2 div w mul add } if def 
  y2 y1 sub h mul 1 add dup 1000 gt { pop 1000 } if 
  { newpath i dy mul dup x0 exch moveto 
    b 0 gt { gsave c i a cvs dup stringwidth pop 
      /z2 ED 
      w 0 gt {z1 z2 add neg} {z1} ifelse 
      h 0 gt {z1} {b neg} ifelse 
      rmoveto %
      i gridstep mod 0 eq { show } { pop } ifelse %show 
      grestore } if 
    dup f exch t moveto 
    i gridstep mod 0 eq {g exch t L stroke } if %g exch t L stroke 
    /i i h add def 
  } repeat 
  grestore 
} def
}
\def\tx@Grid{\psk@gridstep StepGrid }
\makeatother

\psset{unit=1mm}
\addtopsstyle{gridstyle}
{
    gridlabels=3pt,
    gridstep=5
}


\begin{document}
\begin{pspicture}[showgrid](50,50)

\end{pspicture}
\end{document}

Let's see what is going on here...

The original \psgrid macro (in pstricks.tex) sets a bunch of parameters and calls \tx@Grid, defined to be Grid . For completeness, here's a view on the \psgrid hierarchy:

\def\psgrid{\pst@object{psgrid}}
\def\psgrid@i{\@ifnextchar({\psgrid@ii}{\expandafter\psgrid@iv\pic@coor}}
\def\psgrid@ii(#1){\@ifnextchar({\psgrid@iii(#1)}{\psgrid@iv(0,0)(0,0)(#1)}}
\def\psgrid@iii(#1)(#2){\@ifnextchar({\psgrid@iv(#1)(#2)}{\psgrid@iv(#1)(#1)(#2)}}
\def\psgrid@iv(#1)(#2)(#3){%
  \begin@SpecialObj%
    \pst@getcoor{#1}\pst@tempA%  hv 1.11
    \pst@getcoor{#2}\pst@tempB%  hv 1.11
    \pst@@getcoor{#3}%
    \ifnum\psk@subgriddiv>1\relax
      \addto@pscode{
        gsave
        \tx@setStrokeTransparency
        \psk@subgridwidth SLW 
        \pst@usecolor\pssubgridcolor
        \pst@tempB \pst@coor \pst@tempA                 % hv 1.11
%        \pst@number\psxunit \pst@number\psyunit        % hv 1.11
        \pst@number\psxunit abs \pst@number\psyunit abs % hv 1.11
        \psk@subgriddiv\space \psk@subgriddots\space
        {} 0 
        \psk@gridfont findfont 0 scalefont setfont      % hv 1.16
    \tx@Grid 
    grestore
      }%
    \fi%
    \addto@pscode{
      gsave
      \tx@setStrokeTransparency
      \psk@gridwidth SLW 
      \pst@usecolor\psgridcolor
      \pst@tempB \pst@coor \pst@tempA                 % hv 1.11
      \pst@number\psxunit abs \pst@number\psyunit abs % hv 1.11
%      \pst@number\psxunit \pst@number\psyunit        % hv 1.11
      1 \psk@griddots\space { \pst@usecolor\psgridlabelcolor }
      \psk@gridlabels 
      \psk@gridfont findfont \psk@gridlabels scalefont setfont  % hv 1.16
       \tx@Grid 
      grestore
    }%
  \end@SpecialObj}

Note how it actually calls \tx@Grid twice. Once for the subgrid and once for the regular grid. From the definition of \tx@Grid, it should be clear that the actual grid is not typeset in TeX, but in Postscript. So, from pstricks.pro, here is the (Postscript) definition of Grid:

/Grid { 
  newpath 
  /a 4 string def 
  /b ED %               psk@gridlabels in pt
  /c ED %               { \pst@usecolor\psgridlabelcolor }
  /n ED %               psk@griddots
  cvi dup 1 lt { pop 1 } if 
  /s ED %               \psk@subgriddiv
  s div dup 0 eq { pop 1 } if 
  /dy ED s div dup 0 eq { pop 1 } if %  \pst@number\psyunit abs
  /dx ED dy div round dy mul %      \pst@number\psxunit abs
  /y0 ED dx div round dx mul 
  /x0 ED dy div round cvi 
  /y2 ED dx div round cvi 
  /x2 ED dy div round cvi 
  /y1 ED dx div round cvi 
  /x1 ED 
  /h y2 y1 sub 0 gt { 1 } { -1 } ifelse def 
  /w x2 x1 sub 0 gt { 1 } { -1 } ifelse def 
  b 0 gt { 
    /z1 b 4 div CLW 2 div add def
%    /Helvetica findfont b scalefont setfont 
    /b b .95 mul CLW 2 div add def } if 
  systemdict /setstrokeadjust known 
    { true setstrokeadjust /t { } def }
    { /t { transform 0.25 sub round 0.25 add exch 0.25 sub round 0.25 add
       exch itransform } bind def } ifelse 
  gsave n 0 gt { 1 setlinecap [ 0 dy n div ] dy n div 2 div setdash } { 2 setlinecap } ifelse 
  /i x1 def 
  /f y1 dy mul n 0 gt { dy n div 2 div h mul sub } if def 
  /g y2 dy mul n 0 gt { dy n div 2 div h mul add } if def 
  x2 x1 sub w mul 1 add dup 1000 gt { pop 1000 } if 
  { i dx mul dup y0 moveto 
    b 0 gt 
      { gsave c i a cvs dup stringwidth pop 
        /z2 ED w 0 gt {z1} {z1 z2 add neg} ifelse 
    h 0 gt {b neg}{z1} ifelse 
        rmoveto show grestore } if 
    dup t f moveto 
    g t L stroke 
    /i i w add def 
  } repeat 
  grestore 
  gsave 
  n 0 gt
  % DG/SR modification begin - Nov. 7, 1997 - Patch 1
  %{ 1 setlinecap [ 0 dx n div ] dy n div 2 div setdash }
    { 1 setlinecap [ 0 dx n div ] dx n div 2 div setdash }
  % DG/SR modification end
    { 2 setlinecap } ifelse 
  /i y1 def 
  /f x1 dx mul n 0 gt { dx n div 2 div w mul sub } if def 
  /g x2 dx mul n 0 gt { dx n div 2 div w mul add } if def 
  y2 y1 sub h mul 1 add dup 1000 gt { pop 1000 } if 
  { newpath i dy mul dup x0 exch moveto 
    b 0 gt { gsave c i a cvs dup stringwidth pop 
      /z2 ED 
      w 0 gt {z1 z2 add neg} {z1} ifelse 
      h 0 gt {z1} {b neg} ifelse 
      rmoveto show grestore } if 
    dup f exch t moveto 
    g exch t L stroke 
    /i i h add def 
  } repeat 
  grestore 
} def

There are two loops within the definition of Grid (denoted by the {...} repeat blocks). The first sets the vertical grid components (both the rules and the labels), while the second sets the horizontal grid components. Lines are set using the L stroke directive, while strings are displayed using show. The definition of StepGrid in the original MWE mere conditions on whether or not to show/stroke the grid components if (using pseudocode representation) "the current item-to-display" MOD gridstep = 0. In more detail,

%...
rmoveto show grestore } if 
%...

in Grid is updated to

%...
rmoveto %
i gridstep mod 0 eq { show } { pop } ifelse %show 
grestore } if 
%...

in StepGrid, where i is the current iteration and gridstep is the grid step size set using gridstep=<value>. The label is either shown or popped from the stack. Similarly for the rules,

%...
g t L stroke 
%...

in Grid is updated to

%...
i gridstep mod 0 eq { g t L stroke } if %g t L stroke 
%...

in StepGrid. Ultimately the formal definition of \psgrid (and subsidiaries) are kept the same. Only the definition of the Postscript grid-calling function on the LaTeX-side \tx@Grid is updated to now insert the grid step \psk@gridstep, and call the new StepGrid function.


For those interested in more detail when it comes to the Postscript language, consider reading the Postscript Language Reference (in particular, section 8.2 Operator Details, p 524 onward).

share|improve this answer
    
now how am I going to catch you up? –  David Carlisle Oct 24 '13 at 12:22
    
@DavidCarlisle: You had plenty of time to solve this! :D –  Werner Oct 24 '13 at 14:09
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.