9

I'm trying to create a new symbol that simply combines a number with a triangle, by placing the number inside the triangle. It should work in math-mode (text mode would be nice, but not required).

I've tried the following:

\documentclass{article}
\usepackage{graphicx}


\newcommand{\trinum}[1]{%
\triangle\hspace{-.57em}\raisebox{0.1em}{\scalebox{.5}{#1}}
}

\begin{document}

Works fine in default environment
\[
\trinum{1} \qquad \trinum{2} \qquad \trinum{3}
\]

Slightly shifted when used in Huge environment:
{\Huge
\[
\trinum{1} \qquad \trinum{2} \qquad \trinum{3}
\]
}

Slightly shifted when used in tiny environment:
{\tiny % I know, it's too small to read, but just used as a check
\[
\trinum{1} \qquad \trinum{2} \qquad \trinum{3}
\]
}

Messed up when used as subscript or super script    
\[
\trinum{1} \qquad x_\trinum{1} \qquad x^\trinum{1}
\]

\end{document}

result As you can see, it doesn't work nicely for scaled text and very badly for superscript and subscript. I was wondering if there is a better way of doing this.

10

Magic of \ooalign:

\documentclass{article}
\usepackage{graphicx}

\newcommand{\trinum}[1]{\mathpalette\dotrinum{#1}}
\newcommand{\dotrinum}[2]{{%
  \vphantom{\triangle}%
  \ooalign{%
    $#1\triangle$\cr\hidewidth\scaleraise{$#1#2$}\hidewidth\cr
  }%
}}
\newcommand{\scaleraise}[1]{%
  \raisebox{.2\height}{\scalebox{0.5}{#1}}%
}

\begin{document}

Works fine in default environment
\[
\trinum{1} \qquad \trinum{2} \qquad \trinum{3}
\]

Slightly shifted when used in Huge environment:
{\Huge
\[
\trinum{1} \qquad \trinum{2} \qquad \trinum{3}
\]
}

Slightly shifted when used in tiny environment:
{\tiny % I know, it's too small to read, but just used as a check
\[
\trinum{1} \qquad \trinum{2} \qquad \trinum{3}
\]
}

Messed up when used as subscript or super script    
\[
\trinum{1} \qquad x_{\trinum{1}} \qquad x^{\trinum{1}}
\]

\end{document}

I left your original text, except for bracing the subscript and superscript. See https://tex.stackexchange.com/a/22375/4427 for details on \ooalign

enter image description here

  • Thank you, a command like \ooalign is exactly what I was looking for. Unfortunately, sometimes it's hard to find commands if you don't know how they could be called! – JJM Driessen Aug 29 '15 at 11:37
  • Oops. I've just tried to test run your code, I seem to get 101 error messages, starting with many Missing } inserted. I can't seem to retrieve the error. It happens whenever I run the script even when the function is only called once (e.g. \trinum{1}). Without using the function, the errors are not triggered. Changing the engine doesn't affect the problem either. – JJM Driessen Aug 29 '15 at 12:05
  • @JJMDriessen Sorry, I planted a bug. It should be OK now – egreg Aug 29 '15 at 12:44
10

A TikZ solution with works in different sized for both math and text mode.

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

\makeatletter
\newcommand*{\trinum}{}
\DeclareRobustCommand*{\trinum}[1]{%
  \ensuremath{%
    \mathpalette\@trinum{#1}%
  }%
}
\newdimen\trinum@sep
\newdimen\trinum@rule
\newcommand*{\@trinum}[2]{%
  % #1: math style
  % #2: num
  \settowidth\trinum@sep{$\m@th#1\mkern1mu$}%
  \setlength{\trinum@rule}{.8\trinum@sep}
  \tikz\node[
    regular polygon,
    regular polygon sides=3,
    draw,
    line width=\trinum@rule,
    inner sep=\trinum@sep,
  ]{$\m@th#1#2$};
}
\makeatother

\begin{document}
\[
  \trinum{1}\trinum{2}^{
    \trinum{1}\trinum{2}^{
     \trinum{1}\trinum{2}
    }
  }
\]
\begin{center}
  \Huge\trinum{2}
  \huge\trinum{2}
  \LARGE\trinum{2}
  \Large\trinum{2}
  \large\trinum{2}
  \normalsize\trinum{2}
  \small\trinum{2}
  \footnotesize\trinum{2}
  \scriptsize\trinum{2}
  \tiny\trinum{2}
\end{center}
\end{document}

Result

Variation, the numbers can also be made smaller:

\newcommand*{\@trinum}[2]{%
  % #1: math style
  % #2: num
  \settowidth\trinum@sep{$\m@th#1\mkern.5mu$}%
  \settowidth{\trinum@rule}{$\m@th#1\mkern.8mu$}%
  \tikz\node[
    regular polygon,
    regular polygon sides=3,
    draw,
    line width=\trinum@rule,
    inner sep=\trinum@sep,
    scale=.5,
  ]{$\m@th#1#2$};
}
  • This is a nice solution too, and possibly even produces results that look nicer than the regular triangle. Problem is that when using this in conjunction with regular (empty) triangles in math mode, they'll look slightly different, which may come across as sloppy. – JJM Driessen Aug 29 '15 at 11:42

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.