5

I'm designing some icons with TikZ to be used in my text. As a working example for this question let's a take a simple arrow:

\tikz[->]{\draw[>=stealth] (0,0)--(1,0);}

How can I treat this picture as if it was a regular "character" in my text? So that it automatically scales to the surrounding fontsize, color, baselineskips, and so on? For example, if I put my TikZ in a \def\myArrow{\tikz{...}} and use it like

Lorem Ipsum \myArrow Dolorem \dots

I want to behave my drawn arrow just as if it was a regular character. I know that I can use scale and baseline options from the \tikz command and do a lot of my own calculation, but this seems way to complicated and error-prone.

What is the most TeX/LaTeX/TikZ conform way to automatially align and scale "inline" TikZ pictures to the surrounding text?

Foo Bar
  • 13,247
  • 1
    The real answer to this is: make a font so that your icons scale as do the characters in other symbol fonts. This is especially the case if you want a general answer i.e. 'how can I do this for arbitrary complex symbols?' If you only have simple icons, you can manage it reasonably nicely, but if you have a lot or more complex symbols, a font would be easier to use. That said, take a look at tikzsymbols which does something not that dissimilar. [Note that the usual issues concerning nested tikzpictures arise if you use these in tikzpicture environments.] – cfr Jan 27 '16 at 04:00

2 Answers2

3

Here is a first cut for the simple arrow you have proposed. This scales the arrow size and line width relative to the size when the font is 10pt. As in Rmano's answer, I have used em to modify the width. A node is added with a \strut at the end which, coupled with \raisebox{-.3\baselineskip}, allows the vertical placement of the arrow to be defined using the anchor option of the node. The color is already automatic.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\makeatletter
\def\myArrow{%
    \raisebox{-.3\baselineskip}{%
        \tikz{%
            \pgfmathsetmacro{\ratio}{\f@size/(10)}%Calculate the current font size ratio (relative to 10pt)
            \pgfmathsetmacro{\scaledarrowlength}{\ratio*3}%Scale the arrow length for the current font (default is 3pt at 10pt font)
            \pgfmathsetmacro{\scaledarrowwidth}{\ratio*2}%Scale the arrow width for the current font (default is 2 pt at 10pt font)     
            \pgfmathsetmacro{\scaledlinewidth}{\ratio*0.5}%Scale the line width for the current font (default is 0.5 pt at 10pt font)
            \draw[-{Stealth[width=\scaledarrowwidth pt,length=\scaledarrowlength pt]},line width=\scaledlinewidth pt] (0,0)--(1em,0) node[anchor=mid,inner sep=0pt]{\strut};%
            }}}
\makeatother
\begin{document}
    {\tiny\color{red}{Lorem Ipsum \myArrow Dolorem \ldots}}\par
    {\scriptsize\color{orange}{Lorem Ipsum \myArrow Dolorem \ldots}}\par
    {\footnotesize\color{yellow}{Lorem Ipsum \myArrow Dolorem \ldots}}\par
    {\small\color{green}{Lorem Ipsum \myArrow Dolorem \ldots}}\par
    {\normalsize\color{blue}{Lorem Ipsum \myArrow Dolorem \ldots}}\par
    {\large\color{violet}{Lorem Ipsum \myArrow Dolorem \ldots}}\par
    {\Large\color{red}{Lorem Ipsum \myArrow Dolorem \ldots}}\par
    {\LARGE\color{orange}{Lorem Ipsum \myArrow Dolorem \ldots}}\par
    {\huge\color{yellow}{Lorem Ipsum \myArrow Dolorem \ldots}}\par
    {\Huge\color{green}{Lorem Ipsum \myArrow Dolorem \ldots}}\par
\end{document}

result

Guho
  • 6,115
2

You need to adjust the baseline etc., but using ex or em as basic measurement units you can have:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning,calc}
\def\mypic{\tikz[x=1em,y=1em,baseline=-0.5,->]{\draw[>=stealth] (0,0)--(0.5,0.5) --(1,0);}}
\begin{document}

text text \mypic\ text

{\LARGE text \mypic text

}
\end{document}

enter image description here

You will probably need to use some font dimension to adjust baseline and skips, but well --- the idea is here.

And BTW, it seems that the arrow size is not scaling --- the features are probably defined in absolute coordinates (with pt).

Rmano
  • 40,848
  • 3
  • 64
  • 125