6

I am new to using TikZ and have a very simple question that I can't find the answer to online. I am joining line segments in a tikzpicture, but the corner points seem to be missing yielding a notched appearance with thick lines (and when zooming in). A very short code segment is given below:

\begin{tikzpicture}[black,line width = 2pt,scale = 1.0]
\draw (0,0) -- (1,0);
\draw (1,0) -- (1,1);
\draw (1,1) -- (2,1);
\end{tikzpicture}

Because of the way I need to draw my figure I cannot join several points in a cycle. So I need some way of adding points to the 'notches' that gives sharp corners. It seems to me that I should be able to add small rectangular nodes, but I'm not sure of the exact syntax for that so the size of the nodes is the same width as the lines. Thank you for any advice.

  • 2
    Hi, welcome! You need to draw things in one stretch. \draw (0,0) -- (1,0) -- (1,1) -- (2,1);. – user121799 Jul 2 '18 at 2:03
  • 2
    Can you make your code compilable? Can you explain why you can't draw them in one go, as @marmot suggests? – cfr Jul 2 '18 at 2:36
8

Use the line cap option to specify how lines "end" (p.167, pgfmanual, v3.01a).

\documentclass[tikz,border=1mm]{standalone}
\begin{document}
\begin{tikzpicture}[line width=2pt]
  \draw (0,0) -- (1,0);
  \draw (1,0) -- (1,1);
  \draw (1,1) -- (2,1);
  \node[right] at (2,.5) {line cap=butt (default)};
\end{tikzpicture}
\begin{tikzpicture}[line width=2pt,line cap=rect]
  \draw (0,0) -- (1,0);
  \draw (1,0) -- (1,1);
  \draw (1,1) -- (2,1);
  \node[right] at (2,.5) {line cap=rect};
\end{tikzpicture}
\begin{tikzpicture}[line width=2pt,line cap=round]
  \draw (0,0) -- (1,0);
  \draw (1,0) -- (1,1);
  \draw (1,1) -- (2,1);
  \node[right] at (2,.5) {line cap=round};
\end{tikzpicture}
\end{document}

enter image description here

enter image description here

enter image description here

5

If you want to have proper line joins, you need to draw the contour in one stretch. How else would TikZ know that it is supposed to join those lines? However, sometimes it is almost unavoidable to draw the thing in several commands, e.g. when the different stretches get patched together in a macro. Then one can cheat and add an appropriate "arrow".

enter image description here

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}[black,line width = 2pt,scale = 1.0]
\draw (0,0) -- (1,0);
\draw (1,0) -- (1,1);
\draw (1,1) -- (2,1);
\node at (1,1.5) {original};
\begin{scope}[xshift=2.5cm]
\draw (0,0) -- (1,0) -- (1,1) -- (2,1);
\node at (1,1.5) {one stretch};
\end{scope}
\begin{scope}[xshift=5cm,line join=round]
\draw (0,0) -- (1,0) -- (1,1) -- (2,1);
\node[align=center] at (1,1.5) {one stretch\\ \texttt{line join=round}};
\end{scope}
\begin{scope}[xshift=7.5cm]
\draw[{Round Cap[]}-{Round Cap[]},shorten >=-1pt,shorten <=-1pt] (0,0) -- (1,0);
\draw[{Round Cap[]}-{Round Cap[]},shorten >=-1pt,shorten <=-1pt] (1,0) -- (1,1);
\draw[{Round Cap[]}-{Round Cap[]},shorten >=-1pt,shorten <=-1pt] (1,1) -- (2,1);
\node at (1,1.5) {cheating};
\end{scope}
\end{tikzpicture}
\end{document}

UPDATE: Response to your comment below: for your polyominoes I'd recommend to make the lines longer at each end by half of the line width, see the "cheating" example on the right.

enter image description here

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}[black,line width = 2pt,scale = 1.0]
\draw (0,0) -- (1,0);
\draw (1,0) -- (1,1);
\draw (1,1) -- (2,1);
\node at (1,1.5) {original};
\begin{scope}[xshift=2.5cm]
\draw (0,0) -- (1,0) -- (1,1) -- (2,1);
\node at (1,1.5) {one stretch};
\end{scope}
\begin{scope}[xshift=5cm]
\draw[shorten >=-1pt,shorten <=-1pt] (0,0) -- (1,0);
\draw[shorten >=-1pt,shorten <=-1pt] (1,0) -- (1,1);
\draw[shorten >=-1pt,shorten <=-1pt] (1,1) -- (2,1);
\node at (1,1.5) {cheating};
\end{scope}
\end{tikzpicture}
\end{document}
  • Thank you but your solution seems complicated and does not give sharp corners. I think I have figured out what I need. My guess that I should be able to add rectangular nodes appears to be correct, as the code below shows: \begin{tikzpicture}[black,line width = 2pt,scale = 1.0] \draw (0,0) -- (1,0); \node[fill,minimum size=2pt,inner sep = 0pt] at (1,0) {}; \draw (1,0) -- (1,1); \node[fill,minimum size=2pt,inner sep = 0pt] at (1,1) {}; \draw (1,1) -- (2,1); \end{tikzpicture} – Marcus Jul 2 '18 at 2:25
  • Thanks. Sorry for my lack of knowledge. This looks like a path to me. Maybe I should explain what I'm wanting to do. I am using Tikz to draw the edges of polyominoes that tile a region. Because of the large number of pieces involved I want to write a program that first of all loops through the rows to draw all horizontal edges, then loops through all columns drawing the horizontal edges. So I cannot draw 'paths' when I have e.g. 600 pieces in a tiled region. – Marcus Jul 2 '18 at 2:38
  • 2
    @Marcus OK, that explains many things. So if you only have horizontal and vertical segments, the simplest option would probably be to make the ends longer by half the line width, i.e. 1pt in your case, which can be done either by shifting the coordinates or more elegantly by adding [shorten >=-1pt,shorten <=-1pt] to all the \draw commands. – user121799 Jul 2 '18 at 2:42
  • 2
    Maybe the graph syntax would be helpful here? @Marcus – cfr Jul 2 '18 at 2:56
  • Thanks Marmot. This does exactly what I need, so I consider this question closed. I can even define my own newcommands so that the line widths and line width adjustments can be easily adjusted. – Marcus Jul 2 '18 at 22:38

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.