9

My use case is drawing a ticked box in line with normal text like this: "Hello ☒ World." (unicode example, your mileage may vary). One solution I managed to come up with is the following rather verbose expression.

Hello \tikz{
      \node[draw,rectangle,minimum size=1.5ex] {};
      \node[draw,cross out,minimum size=1.5ex] {};
} World.

Is there a more elegant way to describe this picture, for example by having the two shapes superimposed on one node?

Alternatively, is there any cross out path operation akin to rectangle, allowing something like the following?

\tikz \draw rectangle (1.5ex,1.5ex) cross out (0,0);

(N.B. This issue is mostly of aesthetic and academic nature, since the first example does indeed work as expected.)

Clarification: This is single-use so creating a new command is not what I'm after (thanks Peter).

Fritz
  • 6,273
  • 31
  • 55

5 Answers5

7

enter image description here

Some explanations about the question and my answer : A node has only one shape, so to modify a shape associated to a node, you can define a new shape with \pgfdeclareshape and it's possible for example to combine two defined shapes and another solution is to modify the shape used. In the next code I added two lines to the rectangle. It's possible to do that with the path picture option. With this option, it's possible to use some anchors of the bounding box but others solutions are possible.

\documentclass{article}
\usepackage{tikz}

   \tikzset{squarecross/.style={draw,path picture={% 
      \draw[black]
       (path picture bounding box.north west) -- (path picture bounding box.south east) 
       (path picture bounding box.south west) -- (path picture bounding box.north east);
      }}}   

\begin{document}
Hello  \tikz \node[squarecross] (mynode) {}; World.
\end{document} 
Alain Matthes
  • 95,075
  • A bit more verbose than what I was hoping for. Also, it appears that I only have PGF/TikZ 2.00 where /tikz/path picture does not exist. Shame. Still, good to know that technique. – Fritz Mar 08 '12 at 17:23
  • Verbose no ! the style is not really in your code, you use only \tikz \node[squarecross] {};. There are others methods but you need in each way to define something in a style. 2.00 !! it's not serious ! :) Do you have a good reason to not update ? you need to update because there are a lot of interesting improvements. – Alain Matthes Mar 08 '12 at 17:57
  • Not upgrading? Well, mostly due to laziness and not liking Ubuntu 11.10. Of course you're right about the \tikzset not being in the text. It's verbose however, if you think in terms of Code Golf. ;-) – Fritz Mar 08 '12 at 18:42
  • Having thought about my question a bit more, I noticed that having two shapes on one node causes problems like conflicting anchors. So the only viable solution to have "two shapes on one node" is thus painting them yourself, as you have done. If you add that to your answer, I feel I can accept it. – Fritz Mar 21 '12 at 14:22
  • @Fritz Sorry but I have some difficulties to understand your comment because my english is not enough efficace to understand subtleties. But here you can't get conflicting anchors because you have only one node (mynode). The cross does not modify the anchors. The cross completes the rectangle. The pgfmanual says : "the path picture is cleared at the beginning of each path". Can you explain differently the problem ? – Alain Matthes Mar 21 '12 at 16:18
  • Sorry, I'll try to explain more clearly. The problem is in my question, not in your answer. I asked for "two shapes on one node", as in \node[rectangle,cross out]{};. Later I noticed that this can not work because when you write \node[rectangle,circle](test){};, you don't know if (test.north west) is on the circle or on the rectangle. If your answer included something like "No you can't have multiple shapes but this is how you can draw your own shape.", I would happily accept it, so that future visitors can see how to do it. :-) – Fritz Mar 21 '12 at 17:36
  • Certainly , a node has only one shape and in my answer I complete (modify) the shape rectangle but it's not exactly draw my "own shape". For me, draw an own shape it's to use \pgfdeclareshape. – Alain Matthes Mar 21 '12 at 17:54
  • Nice! It doesn't seem to be possible to draw outside of the bounding box with this: ($(path picture bounding box.south west)+(-0.1,-0.1)$) -- ($(path picture bounding box.north east)+(0.1,0.1)$) will still draw only to the drawn node border. Can this be circumvented? – Raphael Aug 27 '12 at 18:21
3

Not sure if this is any better, but you could just draw this shape:

enter image description here

\documentclass{article}
\usepackage{tikz}

\newcommand{\CrossOut}{%
    \tikz [x=1.5ex,y=1.5ex] 
        \draw (0,0) -- (1,0) -- (1,1) -- (0,1) -- cycle
         (0,0) -- (1,1)
         (0,1) -- (1,0)
        ;
}%

\begin{document}
Hello \CrossOut World.
\end{document}
Peter Grill
  • 223,288
  • Sorry, I neglected to mention that the scenario is single-use, so creating a new command is not what I was after. Still, thanks for pointing this out, for the sake of completeness. – Fritz Mar 08 '12 at 17:01
3

Non-Tikz solution, using a \XBox symbol from the wasysym package, which I think is more elegant.

\documentclass{article}
\usepackage{wasysym}
\begin{document}
\CheckedBox  \Square  \Huge \XBox
\end{document}

The advantage of a non-Tikz solution is that the symbol can scale as per the font-size and is much simpler.

enter image description here

yannisl
  • 117,160
  • As far as I know the TikZ picture also scales with the font size when using ex or em lengths because these units are based on font metrics. Still, good to know that there are other ways. – Fritz Mar 21 '12 at 14:59
  • @Fritz Sure I didn't mean that a TikZ solution will not scale. – yannisl Mar 21 '12 at 15:01
  • @Fritz Why I wouldn't favour the TikZ solution is you loading a huge library for what is basically a font issue. Doesn't seem elegant and you did ask ... for unicode or other:) – yannisl Mar 21 '12 at 15:03
  • That's a good point. For people who don't already use TikZ in their document this is probably the best solution. – Fritz Mar 21 '12 at 16:10
2

Yet one more:

\documentclass{article}
\usepackage{amssymb}
\begin{document}
Hello $\boxtimes$ Word
\end{document}

MWE

And a lowercase version without packages:

\documentclass{article}
\def\BoxedTimes{\fboxrule=.04em\fboxsep=-0.11em\fbox{\makebox[.725em]{$\times$}}}
\begin{document}
Hello \BoxedTimes{} Word 
\end{document}

enter image description here

Fran
  • 80,769
2

For the sake of completeness, here's how I did it in the end. After some more thought, the following (based on Peter's answer) was the shortest solution for my single-use ticked box that I could think of.

Hello \tikz[x=1.1ex,y=1.1ex] \draw rectangle (1,1) -- (0,0) (1,0) -- (0,1); World.
Fritz
  • 6,273
  • 31
  • 55