7

I have an automaton in tikz and would like to have some text within a triangle of the right size. How can I do that efficiently?


I have tried using a regular polygon, but I can't shrink it to the right size: minimum height and minimum size have no effect. So is there any parameter that controls the size?

   \begin{tikzpicture}[auto,>=latex]
     \node[state,inner sep=0pt,minimum size=.3cm] at (0,0) (init) {};
     \node[state,inner sep=0pt,minimum size=.3cm] at (-5,-1.3) (idelta)
     {};
    \node[draw,minimum height=.5cm,regular
    polygon,regular polygon sides=3] at (-5,-1.3) {foo};

     \path[->] 
     (init)+(0,.5cm) edge (init)
     (init) edge node[] {bar} (idelta);
   \end{tikzpicture}
  • inner sep=0 maybe? – percusse Jul 8 '13 at 17:49
  • inner sep=0 did make the triangle a bit smaller, but minimum height, minimum size still has no effect. I do need the triangle much smaller. scale does the trick, but also shrinks the text. – DaveBall aka user750378 Jul 8 '13 at 17:52
  • 3
    It can't be smaller than the text box.Unless you make a negative inner sep but that has other complications. Or you can change the apex angle to a large value such that it gets wider and shorter – percusse Jul 8 '13 at 17:53
  • 4
    Remove the node text and add a label: label=center:foo. But you have to take care that the shape border doesn’t overlap with the text as it doesn’t do this automatically anymore, similar to using a negative inner sep. – Qrrbrbirlbel Jul 8 '13 at 17:59
  • @Qrrbrbirlbel: thanks a lot, useful as always :) – DaveBall aka user750378 Jul 8 '13 at 18:39
4

I think this is small enough via kerning (which makes the box quite smaller horizontally) :-)

\documentclass[tikz]{standalone}
\usetikzlibrary{shapes.geometric,automata}
\begin{document}
   \begin{tikzpicture}[auto,>=latex]
   \node[draw,minimum size=0cm,inner sep=0,regular polygon,regular polygon sides=3] at (-5,-1.3) {\kern-0.15emfoo\kern-0.15em};
   \node[draw,minimum size=0cm,inner sep=0,regular polygon,regular polygon sides=3] at (-5,-1.3) {\kern-0.5emfoo\kern-0.5em};
   \end{tikzpicture}
\end{document}

enter image description here

|improve this answer|||||
  • yes they are math spaces, sorry :-) but they are too handy. – percusse Jul 8 '13 at 19:26
  • kerning's a good idea, since the triangle grows with the text (+1). But I favor the tikz solution since I can position the text better within the triangle. – DaveBall aka user750378 Jul 8 '13 at 20:06
  • 2
    @DaveBallakauser750378 This is a Tikz solution. – percusse Jul 8 '13 at 20:09
  • Even though I used a different solution (see my own answer), I've accepted your answer, since it is a simple Tikz solution... – DaveBall aka user750378 Jul 11 '13 at 19:36
3

I don't know tikz, but I'm sure one of these formulations can be stuck into a tikz picture.

\documentclass{article}
\usepackage{stackengine}
\usepackage{scalerel}
\newlength\triwidth
\newcommand\tridelt[1]{%
  \setlength\triwidth{\widthof{#1\ }}%
  \stackengine{-.4\triwidth}{#1\ }%
    {\scaleto{\Delta}{1.73\triwidth}}{O}{c}{F}{F}{L}%
}
\newcommand\trinorm[1]{%
  \setlength\triwidth{\widthof{\ #1\ }}%
  \stackengine{-.2\triwidth}{\ #1\ }
    {\scaleto{\triangle}{1.73\triwidth}}{O}{c}{F}{F}{L}%
}
\parskip 1ex
\begin{document}
\tridelt{x}
\tridelt{xyz}
\tridelt{xyzpdq}

\trinorm{x}
\trinorm{xyz}
\trinorm{xyzpdq}
\end{document}

enter image description here

|improve this answer|||||
2

Using Qrrbrbirlbel comments and answer How can I set the TikZ label anchor explicitly?, I got the following solution:

\newcommand{\triangleText}[4]{%x,y,height,text
        \node[inner sep=0,draw,minimum height=#3,regular
        polygon,regular polygon
        sides=3,label={[anchor=south,inner ysep=.03em]below:\footnotesize #4},anchor=north]
        at (#1,#2) {};
}

with

\makeatletter
\tikzset{anchor/.append code=\let\tikz@auto@anchor\relax}
\makeatother

in the preamble.

triangle-example, e.g. for trees

  • 1
    The label is just a node, so you can use inner ysep=<something smaller then .3333em as well. (If you use this often, you might use an extra style and/or the every label style.) – Qrrbrbirlbel Jul 8 '13 at 19:53

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.