2

I've created some symbols with TikZ (and for some of them used shapes.geometric) to use within text, e. g. this one:

\newcommand{\kttriangle}{\tikz{\node[regular polygon, regular polygon sides=3, draw, fill=black] at (0,0) {};}}

However, it was too big so I scaled it down:

\newcommand{\kttriangle}{\tikz{\node[regular polygon, regular polygon sides=3, draw, fill=black, scale=0.55] at (0,0) {};}}

The vertical alignment of the scaled version didn't look good, so I adjusted that too:

\newcommand{\kttriangle}{\tikz[baseline=-2.5]{\node[regular polygon, regular polygon sides=3, draw, fill=black, scale=0.55] at (0,0) {};}}

TikZ icon

Now it finally looks as intended, but the whole process is rather tedious and has to be repeated for every font size and new icon. Therefore, I'd really like to know if it's possible to

  • automatically scale the tikzpicture to the surrounding font size
  • automatically adjust the baseline / vertical alignment to the surrounding font ?
3
  • One idea would be not to use a node but really draw this triangle using em as unit which scales with the font. Nov 24, 2023 at 8:25
  • 3
    Don't use scale to change the node's size but minimum size (describing the circumference) or ìnner sep (describing the incircle). Here you can use font units as well (ex and em). Though, for a simple triangle, you don't really need a node as @JasperHabicht shows in their answer. Nov 24, 2023 at 8:45
  • Thanks! I know that the triangle is easy to draw manually, but I also need more complex icons (e. g. an equilateral pentagon), which are easier to do with shapes.geometric. Nov 24, 2023 at 13:33

1 Answer 1

4

My suggestion is not to use a node shape to draw such a simple symbol, because TikZ nodes don't scale with the coordinate system in any case.

Instead, I'd just draw the triangle using coordinates with em as unit which is a unit that scales with the font size:

\documentclass{article}
\usepackage{tikz}

\newcommand{\kttriangle}{%
    \tikz{
        \fill (0:0em) -- (0:0.8em) -- (60:0.8em) -- cycle;
    }%
}

\begin{document}

{ \Huge Foo \kttriangle{} bar }

{ Foo \kttriangle{} bar }

{ \footnotesize Foo \kttriangle{} bar }

\end{document}

enter image description here


If you want to use nodes, you can use the same logic. You need to make sure that the node shape is exactly 1em (or some other value with em as unit) high. So you should set its inner sep to 0pt and its minimum height to 1em.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes}

\newcommand{\kttriangle}{%
    \tikz{
        \node[fill, regular polygon, regular polygon sides=3, 
            minimum height=0.9em, inner sep=0pt] at (0,0) {};
    }%
}

\begin{document}

{ \Huge Foo \kttriangle{} bar }

{ Foo \kttriangle{} bar }

{ \footnotesize Foo \kttriangle{} bar }

\end{document}

enter image description here


Finally, although I don't suggest doing this, you can use the scale option, but for this to work, you need to calculate the factor for scaling. You could do this by dividing the current length of 1em by 1cm (which is the default unit in TikZ).

But note that scaling a TikZ environment only scales the coordinate system. If you need to also scale nodes and/or line widths etc. you need to additionally use the options transform shape or transform canvas.

\documentclass{article}
\usepackage{tikz}

\newlength{\emunit}
\newcommand{\kticon}{%
    \setlength{\emunit}{1em}%
    \pgfmathsetmacro{\ktscale}{\the\emunit/1cm}%
    \tikz[scale={\ktscale}]{
        \draw[fill] (0,0) rectangle (0.8,0.8);
    }%
}

\begin{document}

{ \Huge Foo \kticon{} bar }

{ Foo \kticon{} bar }

{ \footnotesize Foo \kticon{} bar }

\end{document}

enter image description here

4
  • Wow, great solutions for the scaling! Thanks a lot @jasper-habicht! Do you also have an idea about the vertical alignment? Basically I need the icons to be centered to the line of text. By default they are "bottom-aligned", which looks OK for a triangle, but very bad for sth. like crosshairs (they are placed too high): ``` \tikz{\draw[thick] (-0.5em,0) -- (0.5em,0) (0,-0.5em) -- (0,0.5em) (0,0) circle radius=0.3em];} ``` Your scaling solution gave me the idea to use [baseline=-0.3em] (instead of -3pt, which only works for a 10pt font), but maybe there's a better solution? Nov 24, 2023 at 13:46
  • @seeplusn00b I guess if you use nodes, you can add yshift=0.1em or similar to it. Would this work? Nov 24, 2023 at 14:15
  • For some reason, setting yshift= doesn't have any effect (tried as node and \tikz command option). Anyway, baseline works fine for now and it also scales with the font size when using em. I was just curious if there's another well-known / more robust solution for my alignment issue. Thanks for your help! Nov 25, 2023 at 7:02
  • @seeplusn00b Sorry, you are right, baseline is the way to go, as we are in an inline context here. Anyways, the key is to use em as unit. Nov 25, 2023 at 9:14

You must log in to answer this question.

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