TikZ Macro for Placing an “X” by Coordinate

I would like to make a TikZ macro that will:

1. Have an optional argument of coordinates
2. Place X centered at those coordinates.

Is this possible?

This is how I put two X's on a 10x10 grid manually. (I need a macro for when I have, say, 40 of them to place and they're all congruent, but more complex than an X.)

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}

\draw[gray] (-5,-5) grid (5,5);

\draw[line width =2pt] % x- and y-axes
(-5,0)--(5,0)
(0,-5)--(0,5);

\draw % Draws X as two paths, the way one normally would.
(1.5,4.5)--(0.5,3.5)
(0.5,4.5)--(1.5,3.5);

\draw % Draws the X as four "paths", all from the center of the X out.
(-3,2)--(-3.5,2.5)
(-3,2)--(-3.5,1.5)
(-3,2)--(-2.5,1.5)
(-3,2)--(-2.5,2.5);

\end{tikzpicture}

\end{document}


How could I write a macro to save myself from having to write out the paths of those X's? Ideally, I'd like something like this:

\newcommand{\X}
[1] % This optional argument would be the coordinates of the center of the X.
{whatever goes in here would use the coordinates above to draw the X}

\X{(3,-2)} % This would place the X at (3,-2).


How can I make this work?

• Is this always gonna be for a X-shaped figure? – Gonzalo Medina Aug 9 '15 at 1:35
• For the purposes of this thread, yes, as it's a simple shape that works for the MWE. Eventually the shapes will be more complex, but I thought I'd start with a relatively simple one and start a new thread if I need one for those more complex shapes later. – WeCanLearnAnything Aug 9 '15 at 1:40

You can use a pic path and define the command:

\documentclass{article}
\usepackage{tikz}

\tikzset{
myex/.pic = {
\draw
(-5mm,-5mm) -- (5mm,5mm)
(-5mm,5mm) -- (5mm,-5mm);
}
}
\newcommand\MyEx[1][(0,0)]{\pic at #1 {myex};}

\begin{document}

\begin{tikzpicture}

\draw[gray] (-5,-5) grid (5,5);

\draw[line width =2pt] % x- and y-axes
(-5,0)--(5,0)
(0,-5)--(0,5);

\pic at (3,3) {myex};
\MyEx
\MyEx[(3,-2)]
\MyEx[(-3,2)]

\end{tikzpicture}

\end{document}


In comments it has been mentioned that the length for the "X" might be a function of the coordinates specified as the origin; here's one simply way to achieve this using the calc library and the let syntax; the length of the "X" equals the length of the vector given as origin for the "X" (more complex dependencies can be achieved, but I selected this just for the example):

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

\newcommand\MyEx[1][(0,0)]{
\draw
let
\p1=#1,
\n1={veclen(\p1)}
in
( $#1 + (225:0.5*\n1)$ ) -- ( $#1 + (45:0.5*\n1)$ )
( $#1 + (135:0.5*\n1)$ ) -- ( $#1 + (-45:0.5*\n1)$ );
}

\begin{document}

\begin{tikzpicture}
\draw[gray] (-5,-5) grid (5,5);

\draw[line width =2pt] % x- and y-axes
(-5,0)--(5,0)
(0,-5)--(0,5);

\MyEx
\MyEx[(0,3)]
\MyEx[(3.5,1.5)]
\MyEx[(1,1)]
\MyEx[(-1,-2)]
\MyEx[(-3,-1)]
\MyEx[(1,-3)]
\MyEx[(-3,3)]
\coordinate (aux) at (4,4);
\MyEx[(aux)]
\end{tikzpicture}

\end{document}


The result:

• Couple questions. (1) Where can I read more about this \tikzset thing? I just read the official TikZ/PGF manual and couldn't make sense of it, e.g the  /,pic= part. (2) Instead of using (-5mm,5mm) as absolute magnitudes, is there any way to make them a function of the optional coordinates entered? – WeCanLearnAnything Aug 9 '15 at 1:57
• @WeCanLearnAnything (1) See Section 18.2 The Pic Syntax. (2) Sure, but you would need to describe in more extent your intent (this seems like a topic for a new question). – Gonzalo Medina Aug 9 '15 at 1:59
• @WeCanLearnAnything Please see my updated answer for an example in which the length depends now on the coordinates specified to place the "X". – Gonzalo Medina Aug 9 '15 at 2:46
• @GonzaloMedina You may use ... let \p1 = #1, \n1={veclen(\p1)} in ... to allow explicit coordinate (as (0,0)) and named coordinate (as (a)). – Paul Gaborit Aug 9 '15 at 6:25
• @PaulGaborit That's a very good suggestion; I've incorporated it into my answer. Thanks. – Gonzalo Medina Aug 9 '15 at 15:31

Another solution with a cross out shaped node from shapes.misc library. In next code, mycross style already fixes node contents={} and uses at={#1} option for setting node's center coordinate. And, as usual, if default settings doesn't like, change them according particular needs.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.misc}

\begin{document}

\begin{tikzpicture}[mycross/.style={cross out, draw,
inner sep=0pt, outer sep=0pt,
minimum size=1cm, node contents={}, at={#1}}]

\draw[gray] (-5,-5) grid (5,5);

\draw[line width =2pt] % x- and y-axes
(-5,0)--(5,0)
(0,-5)--(0,5);

\node[mycross={(2,4)}];
\node[mycross={(0,0)}];
\node[mycross={(-2,-3)}, minimum size=2cm, red, ultra thick];
\node[mycross={(1,-4)}, rotate=10];

\end{tikzpicture}

\end{document}