I defined a command masterTriangle to put inline a triangle created using the tikz library. I use this command many times in my document, which is actually a big book and thus for which I need to use the externalize tikz library. I see some strange behavior, since the produced pdf file has 4 instances of the triangle (the compiled external figure, not document). In my actual document there are more than 200 instances of that triangle after compiling the whole book. Can someone explain why this is happening? This is the MWE:
\documentclass{article}
\usepackage{scalerel} % assembly operator
\usepackage{tikz}
\usepackage{xspace}
\usepackage{scalerel}
\usetikzlibrary{external}
\tikzexternalize[prefix=./]
% define custom inline commands
\tikzset{
masterTriangleImpl/.pic={
\draw (0,0) -- (1,0) -- (0,1) -- cycle;
}}
\def\masterTriangle{\scalerel*{\ensuremath{;\tikzsetnextfilename{masterTriangle}\tikz[x=1em, y=1em, line width=1pt]\pic{masterTriangleImpl};}}{X\rule[-.35ex]{0pt}{1pt}}\xspace}
\begin{document}
This is my \masterTriangle and this is another \masterTriangle.
\end{document}
UPDATE
Following the suggestion of @Qrrbrbirlbel, I just draw the triangle using the pic2e package. This is my solution:
\documentclass{article}
\usepackage{amsmath}
\usepackage{pict2e}
\usepackage{xspace}
\makeatletter
\newcommand{\masterTriangle}{\ensuremath{\mathbin{\mathpalette\masterTriangle@\relax}}\xspace}
\newcommand{\masterTriangle@}[2]{%
\begingroup
\setlength{\dimen@}{\masterElement@width{#1}}%
\sbox\z@{$\m@th#1\sqcup$}%
\setlength{\unitlength}{\wd\z@}%
\raisebox{0.5\dimen@}{%
\begin{picture}(1,1)
\linethickness{\dimen@}%
% \roundcap\roundjoin
\polygon(0.05,0.05)(0.95,0.05)(0.05,0.95)
\end{picture}%
}%
\endgroup
}
\newcommand{\masterElement@width}[1]{%
\ifx#1\displaystyle \fontdimen8\textfont\else
\ifx#1\textstyle \fontdimen8\textfont\else
\ifx#1\scriptstyle 1.1\fontdimen8\scriptfont\else
1.25\fontdimen8\scriptscriptfont\fi\fi\fi 3
}
\makeatother
\begin{document}
This is my \masterTriangle and this is another $\masterTriangle$.
\end{document}
which produces the desired outcome without using tikz.
\scalerelfirst typesets the TikZ picture in a box to find out its size and then it typesets it again resized. Theexternallibrary picks both up and externalizes them both. – Qrrbrbirlbel Dec 14 '22 at 08:12\tikz[baseline=+0pt, line width=1pt] \draw (0,+-.35ex) -- +({width("X")*1pt},0) -- +(0,{width("X")*1pt}) -- cycle;gives you almost the same triangle withoutscalerel. Almost because the linewidth stays at 1pt. – Qrrbrbirlbel Dec 14 '22 at 08:35\def\masterTriangle{\ensuremath{\;\tikzsetnextfilename{masterTriangle}\tikz[baseline=+0pt, line width=1pt] \draw (0,+-.35ex) -- +({width("X")*1pt},0) -- +(0,{width("X")*1pt}) -- cycle;}}? That doesn't work for me. – aaragon Dec 14 '22 at 12:09\ensuremathto avoid that error when not using $. Also, AFAIK, the\xspaceis used for the end of macros right? In any case I'm still trying to figure out why is it that there are so many triangle instances in the produced .pdf. In my main document that number is in the hundreds. – aaragon Dec 14 '22 at 12:49\ensuremathyou already can use it in text and math mode without a problem. (Thoughwidth("X")only picks up the normal uprightXand not the slanted math modeX→width("$X$").) My solution will not scale properly when used in subscripts but that can be fixed. – Qrrbrbirlbel Dec 14 '22 at 13:02\mathchoicebut that has the annoying habit to just typeset all four variants (but only output one) which creates four pages per usage in math-mode. Let's start from the top: Is there a problem with having the externalized PDF double the pages? You could also just typeset these symbols once for every math style and save them in a savebox and/or in a pdf and just include them? – Qrrbrbirlbel Dec 14 '22 at 13:42pict2e. – Qrrbrbirlbel Dec 14 '22 at 13:43