pgf-tikz / pgf

A Portable Graphic Format for TeX
https://pgf-tikz.github.io/
1.14k stars 108 forks source link

Fit does not always capture the full shape #815

Open ghost opened 4 years ago

ghost commented 4 years ago

The fit library establishes the geometry of the fitting node by looking at the west, east, north and south anchors only. This yields the appropriate result in most cases but not in all. An explicit example is the trapezium shape, see the MWE below.

However, I do not necessarily think that one should try to fix this (how?). Yet, it might be worthwhile to mention this in the manual. Ways to obtain the proper fit include adding the "overshooting anchors" explicitly to the fit, or to use local bounding boxes.

Minimal working example (MWE)

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit,shapes.geometric}
\begin{document}
\begin{tikzpicture}
 \node[trapezium,trapezium left angle=75, trapezium right angle=105,align=center,draw] (F1)
  {A nice\\ trapezium\\ wants to\\ be fit};
 \node[fit=(F1),draw,red]{};
 \path (5,0) node[trapezium,trapezium left angle=75, trapezium right angle=105,align=center,draw] (F2)
  {A nice\\ trapezium\\ wants to\\ be fit};
 \node[fit=(F2.top right corner) (F2.bottom left corner),draw,blue]{};
\end{tikzpicture}
\end{document}
Screen Shot 2020-01-07 at 8 49 50 PM
hmenke commented 4 years ago

It's not a bad idea to think about a possible fix. What comes to my mind immediately would be to teach every node shape about its convex hull and use that in the fit, if it is available. Otherwise fall back to east, west, etc.

ghost commented 4 years ago

@hmenke Yes, that would work, of course. It would also mean that one adds all the explanations to the discussion of \pgfdeclareshape. And then there of course already several shapes on the market. It might mean that the maintainers of circuitikz will have many things to do.

Rmano commented 4 years ago

And then there of course already several shapes on the market. It might mean that the maintainers of circuitikz will have many things to do.

;-) yes, could be... support for fit is indeed shaky in circuitikz (sometimes the leads are accounted for, sometimes they're not, ditto for labels). I'll track this issue and if something changes (and I have time) I'll try to sort it out as best as I can.

hmenke commented 4 years ago

@Rmano Right now, if you want to make sure that fit properly encapsulates your shapes, you have to set north, south, east, and west such that the whole shape lies inside the rectangle that these anchors define.

Rmano commented 4 years ago

@Rmano Right now, if you want to make sure that fit properly encapsulates your shapes, you have to set north, south, east, and west such that the whole shape lies inside the rectangle that these anchors define.

Yes, I checked and more or less it works. My problem (and is clearly a problem of internal coherency of circuitikz) is that sometimes lead wires are into the shape and sometimes not; also node content, if it straddles the geographical anchors, is not taken into account (It happens especially with transistors; I should fix it but I fear to break a lot of things).

ghost commented 4 years ago

@Rmano @hmenke Given the shape border rotate issue (see https://tex.stackexchange.com/q/548679) it might indeed be worthwhile to go over all the shapes from the shapes.xyz libraries and (1) add a list of additional fit anchors where necessary and (2) add a warning that the shape border rotate won't work wherever appropriate. (I think it would, in principle, be possible to make shape border rotate work for all shapes but this would be a lot of work, and the results generally questionable.) What do you think?

hmenke commented 4 years ago

(1) Yes. (2) No, I want something generic and extensible that is built into \pgfdeclareshape, so that all existing shapes automatically become compatible.