1

I want to draw \rhd with Tikz. To do this, I get the height and width of \rhd and then use those as x and y in Tikz. I expected this to make Tikz draw exactly \rhd but it draw a larger version of it and I do not understand why.

\documentclass{article}
\usepackage{amssymb}
\usepackage{calc}
\usepackage{tikz}
\begin{document}
    \newlength{\testwidth}
    \newlength{\testheight}
    \newsavebox{\testbox}
    \savebox{\testbox}{$\mathsurround=0pt\rhd$}%
    \settowidth{\testwidth}{\usebox{\testbox}}%
    \settototalheight{\testheight}{\usebox{\testbox}}%
    \pgfmathsetlength{\testheight}{\testheight / 2}%
    \begin{tikzpicture}[x=\the\testwidth, y=\the\testheight]
    \draw (0,1) -- (1, 0) -- (0, -1) -- (0, 1);
    \end{tikzpicture}
    $\rhd$
\end{document}
xavierm02
  • 787
  • You can also use \wd\testbox for the width. This is how \settowidth etc actually get the width. OTOH, the total height is {\dimexpr \ht\testbox+\dp\testbox}. – John Kormylo Feb 27 '20 at 16:18

2 Answers2

2

Like most characters, this one has some white space on both sides. Setting \mathsurround does not remove it. BTW, TikZ has the functions of the calc package built in, you can use width and height instead of doing all the box stuff. (There is also a minor conceptual mistake, you need to also subtract the line width, but the bulk of the discrepancy comes from the white space.)

\documentclass{article}
\usepackage{amssymb}
\usepackage{tikz}
\begin{document}  
    \begin{tikzpicture}[x={width("$\mathsurround=0pt\rhd$")-\pgflinewidth}, y={height("$\mathsurround=0pt\rhd$")-\pgflinewidth}]
    \draw (0,0.5) -- (1, 0) -- (0, -0.5) -- cycle;
    \path (-\pgflinewidth/2,-1.2) 
    node[draw,inner sep=0pt,outer sep=0pt,right=0pt]{$\mathsurround=0pt\rhd$};
    \end{tikzpicture}
\end{document}

enter image description here

0

Here's a document that draws a symbol very similar to \rhd with Tikz (but not exactly similar, as you can see if you zoom enough).

\documentclass{article}
\usepackage{amssymb}
\usepackage{calc}
\usepackage{mathtools}
\usepackage{tikz}
\usepackage{transparent}
\usepackage{xfp}
\begin{document}
    \pdfpkresolution=2000

    \def\xrbls{-0.452} % Ratio of baseline
    \def\xrealwidthratio{0.73} % Relative width (i.e. total width divided by width without spaces)
    \def\xrealheightratio{0.98}
    \def\xleftspaceratio{0.11} % Ratio of white spaces on the left
    \def\xbotspaceratio{-0.005}
    \def\tipshift{-0.02}

    \def\xrightspaceratio{\fpeval{1 - \xrealwidthratio - \xleftspaceratio}}
    \def\xtopspaceratio{\fpeval{1 - \xrealheightratio - \xbotspaceratio}}

    \newlength{\xwidth} % Total width
    \newlength{\xheight}
    \newlength{\xdepth}
    \newlength{\xtotalheight}

    \newbox\rhdbox%
    \savebox{\rhdbox}{$\mathsurround=0pt\rhd$}%
    \setlength{\xwidth}{\wd\rhdbox}
    \setlength{\xheight}{\ht\rhdbox}
    \setlength{\xdepth}{\dp\rhdbox}
    \setlength{\xtotalheight}{\xheight + \xdepth}

    \newcommand{\tikzrhd}{
        \mathrel{\begin{tikzpicture}[line join=round, baseline=(baseline)]% TODO
            \coordinate (baseline) at (0, \dimexpr -\xtotalheight / 2 + \xdepth \relax);
            \coordinate (topleft) at (0, \dimexpr \xtotalheight / 2 \relax);
            \coordinate (botright) at (\xwidth, \dimexpr -\xtotalheight / 2 \relax);
            \useasboundingbox (topleft) rectangle (botright);
            %\draw[yellow, line width=0pt] (topleft) rectangle (botright);
            \coordinate (headtop) at (\dimexpr \xleftspaceratio \xwidth + \pgflinewidth / 2 \relax, \dimexpr \fpeval{0.5 - \xtopspaceratio} \xtotalheight - \pgflinewidth / 2 \relax);
            \coordinate (headbot) at (\dimexpr \xleftspaceratio \xwidth + \pgflinewidth / 2 \relax, \dimexpr \fpeval{-0.5 + \xbotspaceratio} \xtotalheight + \pgflinewidth / 2 \relax);
            \coordinate (headright) at (\dimexpr \fpeval{1 - \xrightspaceratio} \xwidth + \pgflinewidth / 2 \relax, 0);
            \draw[blue] (headtop) -- (headright) -- (headbot) -- (headtop) -- (headright); % , dash pattern=on 0.2pt off 0.2pt
            \end{tikzpicture}}
    }




    \newcommand{\testrhd}{
        \mathrel{\rhd}
    }

    Sizes:\\%
    \newbox\mybox%
    \savebox{\mybox}{$\mathsurround=0pt\tikzrhd$}%
    \color{blue}\the\wd\mybox $\times$ (\the\ht\mybox + \the\dp\mybox)\\
    \savebox{\mybox}{$\mathsurround=0pt\testrhd$}%
    \color{red}\the\wd\mybox $\times$ (\the\ht\mybox + \the\dp\mybox)%

    % https://tex.stackexchange.com/questions/21644/how-do-you-superimpose-two-symbols-over-each-other
    \makeatletter
    \newcommand{\superimpose}[2]{%
        {\ooalign{$#1\@firstoftwo#2$\cr\hfil$#1\@secondoftwo#2$\hfil\cr}}}
    \makeatother

    \newcommand{\maketest}[1]{
        $\mathpalette\superimpose{{\color{blue}\transparent{0.5}#1{\tikzrhd}}{\color{red}\transparent{0.5}#1{\testrhd}}}$
    }

    \newcommand{\overunderline}[1]{
        \overline{\underline{#1}}
    }

    \newcommand{\expa}[1]{
        #1^a
    }

    \maketest\overunderline
    \maketest\expa
    \maketest\boxed
\end{document}
xavierm02
  • 787