5

I am using the empty and filled triangle amssymb but i need a third one. It should look like the \blacktriangleright in the first half and like \rhd in the second half.


MWE

\documentclass{article}
\usepackage{amssymb}
\begin{document}

$\rhd$

$\blacktriangleright$

% half \blacktriangleright and half \rhd

\end{document}

Desired output made in paint:

half black half white triangle

5

A solution, which composes the symbol with a clipped version of \blacktriangleright and the full symbol \rhd. The example uses \mathrel for the result as relational symbol (like \blacktriangleright). If the final symbol should be better a binary symbol (like \rhd), then \mathrel needs to be replaced by \mathbin. Luckily both symbols of package amssymb match in form and dimension of the triangle and in the character widths including side bearings.

\documentclass{article}
\usepackage{amssymb}

\usepackage{trimclip}
\makeatletter
\newcommand*{\halfblacktriangleright}{}%
\DeclareRobustCommand*{\halfblacktriangleright}{%
  \mathrel{% like \blacktriangleright or \mathbin like \rhd
    \mathpalette\@halfblacktriangleright{}%
  }%
}
\newcommand*{\@halfblacktriangleright}[2]{%
  % #1: math style
  % #2: unused
  \rlap{%
    \clipbox{0 0 {.55\width} 0}{$#1\blacktriangleright\m@th$}%
  }%
  \rhd
}
\makeatother

\begin{document}

%$\rhd\quad\blacktriangleright$

$1\halfblacktriangleright 2\quad
\scriptstyle 1\halfblacktriangleright 2\quad
\scriptscriptstyle 1\halfblacktriangleright 2$

\end{document}

Result

Definition variation which uses \@ifdefinable for checking, whether a macro is already defined:

\documentclass{article}
\usepackage{amssymb}

\usepackage{trimclip}
\makeatletter
\@ifdefinable{\halfblacktriangleright}{%
  \@ifdefinable{\@halfblacktriangleright}{%
    \protected\def\halfblacktriangleright{%
      \mathrel{% like \blacktriangleright or \mathbin like \rhd
        \mathpalette\@halfblacktriangleright{}%
      }%
    }%
    \def\@halfblacktriangleright#1#2{%
      % #1: math style
      % #2: unused
      \rlap{%
        \clipbox{0 0 {.55\width} 0}{$#1\blacktriangleright\m@th$}%
      }%
      \rhd%
    }%
  }%
}
\makeatother

\begin{document}

%$\rhd\quad\blacktriangleright$

$1\halfblacktriangleright 2\quad
\scriptstyle 1\halfblacktriangleright 2\quad
\scriptscriptstyle 1\halfblacktriangleright 2$

\end{document}
  • 1
    Very nice! I didnt know about the trimclip package before. Perhaps \newcommand should be \DeclareRobustCommand, because \rlap is not expandable (as \clipbox probably). Thus using \halfblacktriangleright will currently lead to nice surprises when used in section headings. – Henri Menke Jul 19 '16 at 21:02
  • 1
    @HenriMenke Thanks, the updated answer makes it robust and fixes a smaller issue with the number of arguments for \@halfblacktriangleright. – Heiko Oberdiek Jul 19 '16 at 21:09
  • 1
    Now I have a question. Why the empty \newcommand*{\halfblacktriangleright}{}? Does \newcommand do some extra bookkeeping which \DeclareRobustCommand is lacking? And one “optimization” egreg once suggested: use \relax instead of {} as the second argument to \mathpalette — it saves you one token! – Henri Menke Jul 19 '16 at 21:20
  • 1
    @HenriMenke The additional \newcommand checks, whether the macro is already defined. I have appended a version, which instead uses \@ifdefinable and e-TeX's \protected. – Heiko Oberdiek Jul 19 '16 at 21:36
  • 1
    @HenriMenke On the other hand \relax as "empty" argument is not empty, but is one token instead of no tokens. Also \relax is longer to write and less clear to read than {} for an empty argument. – Heiko Oberdiek Jul 19 '16 at 21:39

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.