3

I am currently involved in a project where we're using LaTeX to generate PDF files dynamically. We are looking at a typesetting attribute called "Line Weight" and are looking at how LaTeX handles this. "Line Weight" is as follows.

Suppose we create a line between points A and B. While the points define the start and end of the line, that line will have a certain thickness to it. Our question is, how is that line thickness distributed? Is the body of the line above the AB line, below the AB line, or straddling the AB line? Is this an attribute we can control?

  • 1
    I'm not sure if I understand what you are asking but I think that the answer (in theory, no in TeX) could be: let a segment of line from A to B. Now, parallel segments (in both semi-planes) are taken to make a thick line. – Sigur Jan 09 '20 at 21:24
  • 2
    The LaTeX core hasn't much in-built drawing capabilities. In tikz the line will be on both sides. but you can naturally shift it relative to the coordinates. – Ulrike Fischer Jan 09 '20 at 21:33
  • 1
    @Sigur correct. So if we have a line described by points A and B, and it's 2mm thick. At render time, is there 2mm of line above the line defined by AB, 1mm above and 1mm below, or 2mm below. Thanks! – sergio_101 Jan 09 '20 at 21:35
  • Thanks, @UlrikeFischer . I don't think we need to adjust anything, we just need to be aware of the behavior. – sergio_101 Jan 09 '20 at 21:38
  • This question vaguely reminds me of this question of my own. – JJM Driessen Jan 10 '20 at 09:08

2 Answers2

7

In tikz the line is on both sides:

\documentclass[11pt]{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\draw[yellow,line width = 4pt](0,0)--++(1,0);
\draw[red](0,0)--++(1,0);
\draw[blue,->](0.5,-0.5)--(0,0);
\draw[blue,->](0.5,-0.5)--(1,0);
\end{tikzpicture}

\end{document}

enter image description here

Ulrike Fischer
  • 327,261
  • I do not think that this is something one can attribute to TikZ, nor any of those packages. This is something that is done by some driver (or viewer or whatever you want to call it). If you draw a thick line, TikZ does not “know” its boundaries. So, in a way the question is not well phrased since this is not even specific to TeX. –  Jan 09 '20 at 22:08
  • 1
    @Schrödinger'scat But tikz decides where to start the line, it could also internally shift the coordinate by + 0.5 line width and draw from there. – Ulrike Fischer Jan 09 '20 at 22:21
  • Actually: not really. As I said, for a curve it does not know the boundary, so, yes, it could shift, but then this would not be such that, say, the left boundary of the curve follows the specified coordinates. For instance, if you do \draw[double distance=2em] circle[x radius=3em,y radius=1.2em];, TikZ does not "know" the coordinates of the boundary curves that you see. It would be extremely hard to implement something that follows the a different prescription than what the driver "wants" and looks smooth when you combine paths. –  Jan 09 '20 at 23:31
6

I agree with the statements in Ulrike Fischer's answer but would like to add that there is not really a choice. It is not really decided by the standard LaTeX drawing engines like TikZ, PSTricks or l3draw where the line ends up sitting. Apart from the fact that a convention like above or below would not make much sense because lines can have an angle and merge with curves, it would be next-to-impossible to implement a convention of that sort because the line is not "drawn" by LaTeX but by some driver (or "viewer"). To see what that means, consider a nontrivial example.

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}[font=\sffamily,nodes={text width=3cm,align=center}]
 \begin{scope}[local bounding box=A]
  \draw[double distance=2em] (0,0)  circle[x radius=3em,y radius=1.2em];
  \draw[dashed] (0,0)  circle[x radius=3em,y radius=1.2em];
 \end{scope}
 \path (A.south) node[below=1em]{Boundaries of a thick line.};
 \begin{scope}[xshift=3.5cm,local bounding box=B]
  \clip circle[x radius=3em,y radius=1.2em];
  \draw[line width=2em] (0,0)  circle[x radius=3em,y radius=1.2em];
 \end{scope}
 \path (B.south) node[below=1em]{Efforts to have line on one side of a path.};
 \begin{scope}[xshift=7cm,local bounding box=C]
  \clip circle[x radius=3em,y radius=1.2em] (-4.1em,-2.3em) |- (4.1em,2.3em) |-
  cycle;
  \draw[line width=2em] (0,0)  circle[x radius=3em,y radius=1.2em];
 \end{scope}
 \path (C.south|-B.south) node[below=1em]{Efforts to have line on the other side of a path.};
\end{tikzpicture}
\end{document}

enter image description here

The left inlay shows the boundaries (solid) of a thick line with a certain center (dashed line). TikZ does actually not "know" the coordinates of the outer boundaries. While it is possible to derive them for the case at hand, i.e. for an ellipse, there are no simple expressions that cover the general case. Rather, this is the "hard work" done by the graphics card (when you watch this on the screen). Since we definitely want to have smooth combinations of straight segments and curves, the only reasonable convention is to follow the conventions of the drivers.

The other two inlays indicate how one may conceivably get a line that is drawn only on one side of the path. Yes, in specific situations you can always cook up something that does that but there is no general way to do that for all conceivable paths.

So the bottom-line is that there is not really a choice. For any of these applications, regardless of whether or not they are related to LaTeX, we better become friends with what the drivers give us. Otherwise it will be next-to-impossible to have smooth paths. Therefore, as long as you do not make some big efforts, you can be pretty sure that all these packages follow the convention that at any point of a smooth segment the line extends by the same distance along the normal.

I try to illustrate this by the following animation.

enter image description here

The blue curve is the ellipse one draws, the gray line is the thick line we are interested in. At each point one can look at the tangent (orange) and the normal. The line extends in both directions by the same amount. A subtle point in curves is that they can seem to extend further because of the curvature, i.e. when we try to measure the width we also see some part that comes from an earlier stretch. I am not convinced that the verbal description is optimal, but I want to describe what one sees at the left an right stretches.

P.S. This is the code for the animation.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{decorations.markings}
\begin{document}
\foreach \X in {0,0.025,...,0.975}
{\begin{tikzpicture}[font=\sffamily,nodes={text
width=3cm,align=center},>=stealth,scale=2]
 \path[use as bounding box] (-4.7em,-3em) rectangle (4.7em,3em);
 \draw[line width=3em,gray] (0,0)  circle[x radius=3em,y radius=1.2em];
 \draw[blue,postaction={decorate,decoration={markings,
 mark=at position {\X} with {\draw[orange] (-1,0) -- (1,0);
 \draw[red,|<->|] (0,1.5em) -- (0,-1.5em);}}}](0,0)  circle[x radius=3em,y radius=1.2em];
\end{tikzpicture}}
\end{document}