I'm making a macro to generate a tikzpicture depending on the arguments passed into the macro. I want to be able to draw a \node only if it won't intersect with a specified x coordinate.

So say my blacklisted x coordinate is 1 and I want to place my node at (0.8, 0). The node will have some width depending on the text I put inside it, so it has a chance of intersecting with my x coordinate of 1 (that text is from the macro's arguments, so I won't have one static width I can use each time). I want to be able to control whether the node will be placed depending on if its width will pass over x=1 (if it does, I don't want it to be placed).

My thought was to place an invisible version of the node, get its x coordinate, and place the visible version if it doesn't exceed 1. I just don't know how to actually do that: get the x coordinate (both the left and right sides of the node), and use it as a condition for whether to place a second node.

Is this possible, maybe with some sort of library I'm not aware of? Thanks in advance.

1 Answer 1


Is this what you would like to see?

enter image description here

I used opacity=0.25 to show the initial size of the nodes. If you change that to 0, they will be invisible (they will be considered in the bounding box however). If you place a node with anchor=east exactly at x=1cm, it will not be drawn. If you want those nodes to be drawn too, you can change \ifdim\tempx<1cm to \ifdim\tempx>1cm\else.




    my limited node/.style={
        draw, % Only for debugging. When opacity=0, this can be removed.
        opacity=0.25, % Change this to 0 to maken the nodes invisible.
        node contents={#1}, % This is a different way to set the contents of a node.
        % It is needed here because we want the contents used twice.
        % By using the argument of the style, we can use it as often as we wish.
        append after command={ % This key allows us to execute some additional code.
            \pgfextra{ % This command halts the current path construction to execute some more code
                \pgfextractx\tempx{\pgfpointanchor{\tikzlastnode}{east}} % With \pgfextractx{<dimension>}{<point>} the absolute x coordinate of a point can be saved in the <dimension>.
                % With \pgfpointanchor{<coordinate>}{<anchor>} we return the anchorpoint of a certain node or coordinate.
                \ifdim\tempx<1cm % Here we check if the x coordinate of the east anchor is smaller than 1cm 
                    \node at (\tikzlastnode) [draw] {#1}; % If so, draw the node again

        \draw[red] (1,0) -- (1,-3);
        \node at (0,-0.5) [my limited node={test}];
        \node at (0,-1.5) [my limited node={a longer test that protrudes the forbidden line}];
        \node at (1,-2.5) [my limited node={another test},anchor=east];
  • I ended up using a bunch of hacky let commands to get it but this is so much better! Is there an online documentation or something I can reference to know more about how this all works? Aug 8, 2018 at 8:33
  • @ChristianFigueroa I think you can find it all in the PGF/Tikz manual somewhere, but I don't know where. Maybe I can clarify some specific points that you're interested in?
    – Max
    Aug 8, 2018 at 8:35
  • Well pretty much everything in the \tikzset, but I’ll look for it in the manual. Thanks! Aug 8, 2018 at 8:37
  • 2
    @ChristianFigueroa I added some comments to the code, hope this makes it a bit more clear.
    – Max
    Aug 8, 2018 at 8:45

You must log in to answer this question.

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