pdfliteral stroke shape after transformation

Is it possible to modify coordinates in PDF while keeping the stroke square-shaped?

My problem is the following. If I use code like this:

\pdfliteral{q
0.5 w
1 0 0 3 0 0 cm
0  0 m
0   20 l
50  20 l
0   0  l
S
Q
}


then the resulting image

does not look very aesthetic since the brush is shaped as a 1×3 rectangle instead of a square, so the lines are of different thickness. Is it possible to fix it somehow?

An obvious solution is not to use such a transformation of course. But since TeX does not provide easy tools to perform rational arithmetics, I thought that using transformation in PDF would be more elegant.

• Welcome to the site. Could you provide a working example that demonstrates the issue, rather than just a snippet of code? Dec 10 '18 at 10:35
• Well, the code is a working example. Just add \bye to the end and run with pdftex. I've added the resulting image. Dec 10 '18 at 10:59
• I guess it is, if you add \bye to the end. At first, I failed to realize it was pdftex, but I see you do tag it as such. Dec 10 '18 at 11:22
• @Daniel The PDF Reference v1.7 (sec. 5.3.2, sub title Line Width) says "If the CTM (current transformation matrix, which is changed by operator cm in your example) specifies scaling by different factors in the horizontal and vertical dimensions, the thickness of stroked lines in device space will vary according to their orientation." Hence it is 1 0 0 3 0 0 cm that changes the line width. Maybe you need to pre-compute the transformed coordinates used in xpos ypos l (lineto operator). Nov 19 '19 at 22:50

OpTeX (OPmac macros for LuaTeX) supports arithmetic in \pdfliteral argument. Your example should look like:

\pdfliteral{q
0.5 w
0  0 m
0  \_expr{3*20} l
50 \_expr{3*20} l
0   0  l
S
Q
}


tikz handles this quite fine:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\pdfliteral{q
0.5 w
1 0 0 3 0 0 cm
0  0 m
0   20 l
50  20 l
0   0  l
S
Q
}

\begin{tikzpicture}[y=3cm]
\draw (0,0)--(0,2)--(5,2)--cycle;
\end{tikzpicture}
\end{document}


• Thanks for pointing me to the tikz package. Using PGF instead of pdfliteral is probably a good idea. Anyway, I am still interested in answering the former question, but I am afraid that answer is no, it is not possible to fix the issue in some easy way. Dec 11 '18 at 11:58