I think I have a problem about how the box created by tikzpicture is positioned with respect the rest of the text. I have read Positioning TikZ pictures and I think I have understood how the vertical alignment works, but I can't find an equivalent for the horizontal one.

Consider the MWE:

\def\go[#1]{% does not work with current bounding box
        \draw   (0,0) node (sw) {sw} -- 
                (1,0) node (se) {se} -- 
                (1,1) node (ne) {ne} --
                (0,1) node (nw) {nw} -- cycle;}

A\go[overlay, red, baseline=(sw)]
B\go[overlay, blue, baseline=(se)]
C\go[overlay, green, baseline=(ne)]
D\go[overlay, black,  baseline=(nw)]

A\go[overlay, red, baseline=(current bounding box.south west)]
B\go[overlay, blue, baseline=(current bounding box.south east)]
C\go[overlay, green, baseline=(current bounding box.north east)]
D\go[overlay, black,  baseline=(current bounding box.north west)]


Which gives:

Result for the above MWE, 8 colored squares

I have two doubts here:

  1. why the two lines are not the same?

  2. I am trying to have the squares with the east reference (blue and green) before the letter B and C (given that the overlay figure has no size, I want it to extend before the insertion point). I understand that this is expected and correct for the baseline case (it just shifts the box vertically); but... is there an equivalent for the horizontal case?

    To be more specific: I can move the figure with \llap or \rlap or similar tricks, but I was wondering: when tikz smashes the size of the picture to zero, it is like it is collapsing it to a point; is there a way to decide where this point is? I tried anchor too, but it works for the objects in the picture, not the picture itself.

  • 1
    Part of the problem you see is being caused by the use of overlay. In your pictures add the line \draw (current bounding box.south west) -- (current bounding box.north east); and you will begin to see why you attempts to set the baseline as you are isn't working.
    – A.Ellett
    Apr 26 '15 at 20:50
  • It could be a matter of WHEN the current bounding box is used by [baseline=]. Before the picture is drawn, the current bounding box doesn't contain anything. Afterward with should also contain both the rectangle and all the nodes (text and inner sep), which it obviously doesn't. Apr 27 '15 at 3:24

This might be a solution for you. I'm not sure how to achieve the effect you want using just one TikZ picture. So, you might get the idea to nest the TikZ pictures. But if you do a search on nesting tikzpictures, you find plenty of opinion expressed that this is not a good idea. So instead of directly nesting the TikZ pictures, we smuggle the one in to the other by means of a \usebox construction.

I'm not sure what you want to do with the structures you're creating, so this may not be a solution that will work for you, but here it goes...


\def\go[#1]{% does not work with current bounding box
        \draw   (0,0) node (sw) {sw} -- 
                (1,0) node (se) {se} -- 
                (1,1) node (ne) {ne} --
                (0,1) node (nw) {nw} -- cycle;


%% "content"    will hold the smuggled in tikz picture.  It should be wrapped in 
%%              brackets to prevent the key parser from misinterpreting comma.   
%% "anchor"     will specify how to anchor the node for the second tikz picture  
%% "x|yoffset"  will allow you to fine tune the placement of the content         
  content/.store in=\ae@anchor@picture@content,
  anchor/.code={\def\ae@anchor@picture@node@anchor{node [outer sep=0pt,inner sep=0pt,anchor=#1]}},
  xoffset/.store in=\ae@anchor@xoffset,
  yoffset/.store in=\ae@anchor@yoffset,

%% everything will be placed in a group to localize values to 
%% current instance.
    \tikz[remember picture,overlay] 
      \path (0,0) -- ++(\ae@anchor@xoffset,\ae@anchor@yoffset)
      \ae@anchor@picture@node@anchor {\usebox\aebox};%%



A\aeanchorpicture{content={\go[red]},   anchor=south west, xoffset=-2pt,yoffset=-2pt}%%
B\aeanchorpicture{content={\go[blue]},  anchor=south east}%%
C\aeanchorpicture{content={\go[green]}, anchor=north east}%%
D\aeanchorpicture{content={\go[black]}, anchor=north west}%%


enter image description here

  • Yes, I finally used a similar solution, with llap and rlap. I was just wondering if there were a more direct way...
    – Rmano
    Apr 27 '15 at 5:39

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.