54

The below is part of my TikZ picture:

\begin{tikzpicture}
\draw (0,0)--(10,0);
\filldraw (0,0) circle (3pt);
\end{tikzpicture}

I want to place a "square mark" to denote the point at (10,0). How can I place a square mark? Is there anything like

\filldraw (10,0) square (3pt);

In addition, how may I place a "triangle mark"?

Chang
  • 9,528

1 Answers1

60

Three solutions: the first one, using the basic shapes circle and rectangle, and the regular polygon shape from the shapes library (as in Peter Grill's comment). The second and third ones use the plotmarks library; in the second one, the line and the marks are drawn independently (the marks are placed using \nodes with \pgfuseplotmark); in the third solution, the plot coordinates syntax is used, together with the mark=<mark> option (as in Christian Feuersänger's comment):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes}
\usetikzlibrary{plotmarks}

\begin{document}

\begin{tikzpicture}
\draw (0,0)--(10,0);
\filldraw (0,0) circle (3pt);
\filldraw ([xshift=-2pt,yshift=-2pt]10,0) rectangle ++(4pt,4pt);
\node[fill=black,regular polygon, regular polygon sides=3,inner sep=1.5pt] at (5cm,0) {};

\draw (0,-1)--(10,-1);
\node at (0,-1) {\pgfuseplotmark{*}};
\node at (5cm,-1) {\pgfuseplotmark{triangle*}};
\node at (10cm,-1) {\pgfuseplotmark{square*}};

\draw[mark=*] plot coordinates {(0,-2)} -- plot[mark=triangle*] coordinates {(5cm,-2)} --
  plot[mark=square*] coordinates {(10cm,-2)};
\end{tikzpicture}

\end{document}

enter image description here

The following code shows how to change the size and color of the marks for the second and third solutions:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{plotmarks}

\begin{document}

\begin{tikzpicture}
\draw (0,-1)--(10,-1);
\node[mark size=3pt,color=red] at (0,-1) {\pgfuseplotmark{*}};
\node[mark size=5pt,color=blue] at (5cm,-1) {\pgfuseplotmark{triangle*}};
\node[mark size=4pt,color=olive] at (10cm,-1) {\pgfuseplotmark{square*}};

\draw[mark=*,mark size=3pt,mark options={color=olive}] plot coordinates {(0,-2)} 
  -- plot[mark=triangle*,mark options={color=blue}] coordinates {(5cm,-2)} 
  -- plot[mark=square*,mark size=4pt,mark options={color=red}] coordinates {(10cm,-2)};
\end{tikzpicture}

\end{document}

enter image description here

Gonzalo Medina
  • 505,128
  • @Gonzalo Medina, it seems to me that your second solution, which uses \pgfuseplotmark, draws the marks a bit off. Is there a way to fix that? I am really interested in a way to use \pgfuseplotmark on the correct position.

    Note: I see that only \pgfuseplotmark has this problem, if you put a text in the node it gets centered correctly.

    – Nicola Oct 13 '13 at 19:39
  • @Nicola This is now discussed at http://tex.stackexchange.com/q/144810/15925 – Andrew Swann Nov 13 '13 at 14:46
  • I can't comment so I comment here: @Gonzalos Medina's answer; when I do as you suggest it seems the markings (squares,circles, etc.) are off by 0.05 in the x-direction. Very strange. – Physics_maths Nov 13 '13 at 13:40
  • The \pgfuseplotmark command should not be used as text! "Most, but not all, commands of the pgf package must be given within a {pgfpicture} environment." (pgf manual, v3.0, p.973) – Paul Gaborit Sep 24 '14 at 21:02
  • 1
    Isn't the circle (10px) syntax deprecated in favor of circle [radius=10px]? – Clément Jan 06 '17 at 00:46