4

Trying things out without relying on pstricks to see if I can write my own postscript code embedded within a LaTeX document.

\documentclass{article}

\begin{document}

Hello world! 
  \special{ps:
     /inch { 72 mul } def
     gsave
     0 0 1 setrgbcolor
     1 inch 0      rlineto
     0      1 inch rlineto
    -1 inch 0      rlineto
     closepath
     4 setlinewidth
    stroke
    grestore
    }%%
Followed by something else.

\rule{1in}{4pt}

\end{document}

The box is placed where I expect it to be, but the dimensions are all wrong:

enter image description here

But I can easily write an eps file

%!PS-Adoboe-3.0 EPSF-3.0
%%BoundingBox: 0 0  72 72
/inch { 72  mul } def
newpath
0 0 moveto
1 inch 0      rlineto
 0      1 inch rlineto
-1 inch 0      rlineto
closepath
stroke
showpage

And import it to get a square of the right size

\documentclass{article}
\usepackage{graphicx}

\begin{document}

Hello world! 
Followed by something else.

\includegraphics{square}

\rule{1in}{4pt}

\end{document}

resulting in

enter image description here

So what do I need to do to get the dimensions correct within \special{ps:....}?

I've tried looking through pstricks.sty, pstricks.tex, and pstricks.con but not finding anything useful to explain why pstricks is able to get things right, but I'm not.

By the way, with both of the above examples I'm compiling via

latex --> dvips --> ps2pdf
  • PostScript is a scalable format 1 1 moveto doesn't mean move 1pt it means move by 1 unit in the current coordinate matrix – David Carlisle May 6 '15 at 22:18
  • @egreg I made the code for both the embedded \special and eps file better resemble each other. – A.Ellett May 6 '15 at 22:19
  • @DavidCarlisle According to my PostScript document 72 should equal an inch. It seems to do it fine out of the box for the eps file, why not in the \special? How do I set the coordinate matrix to be what I want? – A.Ellett May 6 '15 at 22:20
  • 72 only equals 1in at the start: if the line above is 2 2 scale then it means 2 in. If you use ps: rather than " as the special prefix it is inserted into the postscript that dvips is constructing so is influenced by any changes to the graphics state dvips has made (and it makes a lot) – David Carlisle May 6 '15 at 22:23
  • 1
    texdoc dvips, page 30:-) – David Carlisle May 6 '15 at 22:30
6

If you use " instead of ps: then dvips resets the graphics state before inserting the code:

\documentclass{article}

\begin{document}

Hello world! 
  \special{"
     /inch { 72 mul } def
     gsave
     newpath
     0 0 moveto
     0 0 1 setrgbcolor
     1 inch 0      rlineto
     0      1 inch rlineto
    -1 inch 0      rlineto
     closepath
     4 setlinewidth
    stroke
    grestore
    }%%
Followed by something else.

\rule{1in}{4pt}

\end{document}

with ps: you get whatever the current coordinate matrix is (which you can query from PostScript if you really need it)

| improve this answer | |
  • So it would seem that the gsave and grestore are unnecessary when calling with \special{". – A.Ellett May 6 '15 at 22:38
  • @A.Ellett yep, using " is like including an eps file, there is a save restore and a graphics reset implicitly wrapped around the code. try the \special two forms and compare the generated ps file it's quite readable (or at least I used to read it often 20 years ago while writing the graphics package:-) – David Carlisle May 6 '15 at 22:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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