3

The following code

\documentclass{article}
\usepackage[margin=0.5cm]{geometry}
\usepackage{tikz,wasysym}
\usetikzlibrary{shapes, positioning}

\newcommand{\argument}[2][]{\tikz[baseline]{\node[rectangle, rounded corners=2mm, draw, inner sep=3pt, anchor=base, minimum width=1.3em, #1] {#2};}}

\newcommand{\mygrid}{\tikz{\draw[step=2.5mm] (0,0)  grid (1,1);}}

\begin{document}
    \begin{tikzpicture}
    \node[rectangle split, rectangle split horizontal, thick, draw, rectangle split parts=2, anchor=base] 
    {
        S \argument{19} 
        \nodepart{two}
        \mygrid{}
    };
    \end{tikzpicture}
\end{document}

generates this picture:

enter image description here

I would like that the grid covers entirely (whitout border) the right rectangle (nodepart(two)). I tried using \node[inner sep=0.... but some border remains between the external square and the grid. How to fix this?

P.S. I know I have to avoid nesting tikz pictures. I'm a beginner, this would be the next step.

1
  • Huh, with inner sep=0, removing the [baseline] option on \argument fixes the extra margin problem (but of course messes up the alignment of S and 19).
    – Emma
    Oct 13, 2016 at 17:08

3 Answers 3

0

As I commented above, the extra space looks to me like some issue with nesting tikzpictures and the baseline option. I have no idea how to fix that.

The best alternative I've come up with using rectangle split nodes is to first make the node basically the size you want (by putting an empty box in it) and then draw the grid over it using the corners as anchors. For example,

\documentclass{article}
\usepackage[margin=0.5cm]{geometry}
\usepackage{tikz,wasysym,adjustbox}
\usetikzlibrary{shapes, positioning, shapes.multipart, calc}

\newcommand{\argument}[2][]{\tikz[baseline]{\node[rectangle, rounded corners=2mm, draw, inner sep=3pt, anchor=base, minimum width=1.3em, #1] {#2};}}
\newcommand{\spacer}[2]{\tikz{\path (0,0) rectangle (#1,#2);}}
\newcommand{\fitgrid}[5][]{
  % usage: \fitgrid[options]{south west}{north east}{xcells}{ycells}
  \begin{pgfinterruptboundingbox}
  \newdimen\XCoord
  \newdimen\YCoord
  \coordinate (diff) at ($#3 - #2$);
  \path (diff); \pgfgetlastxy{\XCoord}{\YCoord}
  \begin{scope}[shift={#2}, x={\XCoord}, y={\YCoord}]
    \draw[#1] (0,0) grid[xstep={\XCoord/#4}, ystep={\YCoord/#5}] (1,1);
  \end{scope}
  \end{pgfinterruptboundingbox}
}


\begin{document}
  \begin{tikzpicture}
    \node[rectangle split, rectangle split horizontal, draw, rectangle split parts=2, anchor=base, inner sep=3pt] (mybox)
    {
        S \argument{19}
        \nodepart{two}
        \spacer{1}{1}
    };
    \fitgrid{(mybox.one split south)}{(mybox.north east)}{4}{4}
  \end{tikzpicture}
\end{document}

example

Some notes:

  • The grid is not quite square (nor is it quite exactly the size of the spacer), since the extra margins are still there, just covered up.
  • The grid border doesn't quite line up with the split rectangle node. Not sure why not.
  • This precise implementation breaks if the tikzpicture is scaled.
5
  • Thank you Emma for your help and sorry for the late answer. I tested both the solutions and played a bit with them. The grid shows correctly if I don't add any other information on it. But if I have to write on it like in the following image postimg.org/image/ny2kajm29 then it become very difficult. The grid I need, are just some help lines, it would be ok that the whole paper be squared too. I try to put the grid just in the right rectangle because I thought it was more easy having an aligned grid inside the rectangle than on the whole paper.
    – Spiffhero
    Oct 17, 2016 at 13:57
  • What exactly do you want to show?
    – Emma
    Oct 17, 2016 at 14:24
  • Left and right rectangle should be able to show the same thing inside a grid.
    – Spiffhero
    Oct 17, 2016 at 16:35
  • So what is wrong with the picture you have? Is it just that the grid is on top of the other contents? Or do you want the grid to be square? If the grid has to be square, do the corners of the node rectangle have to line up with the grid?
    – Emma
    Oct 17, 2016 at 16:48
  • Ciao Emma, sorry if I reply late. When I tested your code few days ago, I didn't do the things correctly. I had the time to try it again only now, and it works great! Thanks a lot! Only thing I don't like it's complex.... Thanks again!
    – Spiffhero
    Oct 22, 2016 at 9:57
2

I would prefer styles to commands like in the following:

\documentclass{article}
\usepackage[margin=0.5cm]{geometry}
\usepackage{tikz,wasysym}
\usetikzlibrary{shapes, positioning}

\begin{document}
    \begin{tikzpicture}[
        argument style/.style={rectangle, rounded corners=2mm, draw, inner sep=3pt, anchor=base, minimum width=1.3em},
    ]
        \draw[step=2.5mm] (0,0)  grid (1,1);
        \draw (0,0) rectangle (-1,1);

        \node at (-.8,.5) (text) {S};
        \node[argument style,right=-1pt of text] {19};
    \end{tikzpicture}
\end{document}

enter image description here

2
  • Your output is correct but my code is just an example. I have to be able to change the contents of the first rectangle without need to specify the first rectangle size and the node position.
    – Spiffhero
    Oct 12, 2016 at 9:43
  • ... and the logical structure should be as neat as possible. In the original source code I wrote 'S \argument{19} \nodepart{two} \mygrid{}'
    – Spiffhero
    Oct 12, 2016 at 9:46
1

I'm leaving my other answer because it might be useful for other purposes, but I've found another solution that's closer to what you started with:

\documentclass{article}
\usepackage[margin=0.5cm]{geometry}
\usepackage{tikz,wasysym,adjustbox}
\usetikzlibrary{shapes, positioning}

\newcommand{\argument}[2][]{\tikz[baseline]{\node[rectangle, rounded corners=2mm, draw, inner sep=3pt, anchor=base, minimum width=1.3em, #1] {#2};}}

\newcommand{\mygrid}{\tikz[baseline=.5cm]{\draw[step=2.5mm] (0,0)  grid (1,1);}}

\begin{document}
    \begin{tikzpicture}
    \node[rectangle split, rectangle split horizontal, thick, draw, rectangle split parts=2, anchor=base, inner sep=3pt]
    {
        S \argument{19}
        \nodepart{two}
        \marginbox{-3pt}{\mygrid}
    };
    \end{tikzpicture}
\end{document}

example

The changes are:

  • Explicitly set the inner sep of the node so that you can cancel it out exactly with a \marginbox around the grid. (This requires the adjustbox package.)
  • Add baseline=.5cm as an option for the grid. Somehow, specifying the baseline seems to fix the extra space just as removing it from \argument does. The value of .5cm also gets around the fact that the \marginbox always includes the baseline (so to be safe you should set it in the middle of the grid).

For some reason, the left edge of the grid still doesn't quite line up, but it's probably close enough.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .