8

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}
DaveBall aka user750378
  • 1,098
  • 1
  • 12
  • 29
  • inner sep=0 maybe? – percusse Jul 08 '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 08 '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 08 '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 08 '13 at 17:59
  • @Qrrbrbirlbel: thanks a lot, useful as always :) – DaveBall aka user750378 Jul 08 '13 at 18:39
  • with label=center:foo, the text is exactly in the center of the triangle, which therefore has to be larger to surround the text. with label=south:foo, the text is below the triangle. How can I get it within the triangle, but close to the bottom? – DaveBall aka user750378 Jul 08 '13 at 18:41
  • @DaveBallakauser750378 Ah, I wasn’t sure what you actually wanted to achieve visually. But you can set the anchor of the label explicitly label={[anchor=south]below:foo} (or use the inside key) with the little hack from How can I set the TikZ label anchor explicitly? – Qrrbrbirlbel Jul 08 '13 at 19:04
  • @Qrrbrbirlbel: yes, that works, thank you. I have posted an answer from your comments, with an additional question at the end (how to move the text even further down). If you happen to know an answer, I'd be thankful. – DaveBall aka user750378 Jul 08 '13 at 19:42

3 Answers3

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

percusse
  • 157,807
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

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

DaveBall aka user750378
  • 1,098
  • 1
  • 12
  • 29
  • 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 08 '13 at 19:53