TransWikia.com

How to create a custom decorator symbol in math mode (like tilde)

TeX - LaTeX Asked by Bastian on February 25, 2021

I need some custom decorator symbols, like tilde in math mode.

The package MnSymbol has some neat symbols so I figured I would import these as suggested here:

documentclass{article}
usepackage[utf8]{inputenc}
usepackage[T1]{fontenc}
usepackage{amsmath}
usepackage{amsfonts}
usepackage{amssymb}
%usepackage{accents}
usepackage{bm}

% import certain symbols from MnSymbol,
% https://tex.stackexchange.com/questions/450691/create-diamond-with-a-dash-in-it/450703#450703
defImportFromMnSymbol#1{%
    DeclareFontFamily{U} {MnSymbol#1}{}
    DeclareFontShape{U}{MnSymbol#1}{m}{n}{ <-6> MnSymbol#15 <6-7> MnSymbol#16 <7-8> MnSymbol#17 <8-9> MnSymbol#18 <9-10> MnSymbol#19 <10-12> MnSymbol#110 <12-> MnSymbol#112 }{}
    DeclareFontShape{U}{MnSymbol#1}{b}{n}{ <-6> MnSymbol#1-Bold5 <6-7> MnSymbol#1-Bold6 <7-8> MnSymbol#1-Bold7 <8-9> MnSymbol#1-Bold8 <9-10> MnSymbol#1-Bold9 <10-12> MnSymbol#1-Bold10 <12-> MnSymbol#1-Bold12 }{}
    DeclareSymbolFont{MnSy#1} {U} {MnSymbol#1}{m}{n} }
newcommandDeclareMnSymbol[4]{DeclareMathSymbol{#1}{#2}{MnSy#3}{#4}}
ImportFromMnSymbol{C}
DeclareMnSymbol{smalltriangleup}{mathbin}{C}{73}

begin{document}

$
      tilde{f} 
quad tilde{bm{f}}
quad overset{smalltriangleup}{f}
quad stackrel{smalltriangleup}{f}
$

end{document}

My issue is that I’m clueless how to get the symbol positioned correctly:

output without accents package

The last attempt is in the right direction visually, but the method does not seem to be right, because when highlighting and copying the text from the pdf one gets a triangle symbol followed by an f, instead of a plain f.

So how is it done properly?
It is nice that Amsmath figures out how to position the tilde symbol pleasingly, is there a way to take advantage of that?

I remember the accents package being recommended, but with that package loaded not even the tilde is properly positioned any more:

output with accents package

2 Answers

Why not declaring the symbol as an accent?

documentclass{article}
usepackage[utf8]{inputenc}
usepackage[T1]{fontenc}
usepackage{amsmath}
usepackage{amsfonts}
usepackage{amssymb}
%usepackage{accents}
usepackage{bm}

% import certain symbols from MnSymbol,
% https://tex.stackexchange.com/questions/450691/create-diamond-with-a-dash-in-it/450703#450703
newcommandImportFromMnSymbol[1]{%
  DeclareFontFamily{U}{MnSymbol#1}{}
  DeclareFontShape{U}{MnSymbol#1}{m}{n}
    { <-6>    MnSymbol#15
      <6-7>   MnSymbol#16
      <7-8>   MnSymbol#17
      <8-9>   MnSymbol#18
      <9-10>  MnSymbol#19
      <10-12> MnSymbol#110
      <12->   MnSymbol#112
    }{}
  DeclareFontShape{U}{MnSymbol#1}{b}{n}
    { <-6>    MnSymbol#1-Bold5
      <6-7>   MnSymbol#1-Bold6
      <7-8>   MnSymbol#1-Bold7
      <8-9>   MnSymbol#1-Bold8
      <9-10>  MnSymbol#1-Bold9
      <10-12> MnSymbol#1-Bold10
      <12->   MnSymbol#1-Bold12
    }{}
  DeclareSymbolFont{MnSy#1}{U}{MnSymbol#1}{m}{n}
  SetSymbolFont{MnSy#1}{bold}{U}{MnSymbol#1}{b}{n}
}
newcommandDeclareMnSymbol[4]{DeclareMathSymbol{#1}{#2}{MnSy#3}{#4}}
newcommandDeclareMnAccent[4]{DeclareMathAccent{#1}{#2}{MnSy#3}{#4}}

ImportFromMnSymbol{C}
DeclareMnAccent{acctriangleup}{mathord}{C}{73}

begin{document}

$acctriangleup{f}+tilde{f}$

{boldmath$acctriangleup{f}+tilde{f}$}

end{document}

I reformatted the code for readability (I'm not sure about the intervals, though, but it's not a big deal) and added the setup for the bold math version.

enter image description here

Here's how you can scale down the requested font. I added some arguments in order to allow for loading the same font at different scale factors.

documentclass{article}
usepackage[utf8]{inputenc}
usepackage[T1]{fontenc}
usepackage{amsmath}
usepackage{amsfonts}
usepackage{amssymb}
%usepackage{accents}
usepackage{bm}

% import certain symbols from MnSymbol,
% https://tex.stackexchange.com/questions/450691/create-diamond-with-a-dash-in-it/450703#450703
begingroupmakeatletternfss@catcodes
gdefImportFromMnSymbolaux#1#2#3{%
  DeclareFontFamily{U}{MnSymbol#2#3}{}
  DeclareFontShape{U}{MnSymbol#2#3}{m}{n}
    { <-6>    s*[#1] MnSymbol#25
      <6-7>   s*[#1] MnSymbol#26
      <7-8>   s*[#1] MnSymbol#27
      <8-9>   s*[#1] MnSymbol#28
      <9-10>  s*[#1] MnSymbol#29
      <10-12> s*[#1] MnSymbol#210
      <12->   s*[#1] MnSymbol#212
    }{}
  DeclareFontShape{U}{MnSymbol#2#3}{b}{n}
    { <-6>    s*[#1] MnSymbol#2-Bold5
      <6-7>   s*[#1] MnSymbol#2-Bold6
      <7-8>   s*[#1] MnSymbol#2-Bold7
      <8-9>   s*[#1] MnSymbol#2-Bold8
      <9-10>  s*[#1] MnSymbol#2-Bold9
      <10-12> s*[#1] MnSymbol#2-Bold10
      <12->   s*[#1] MnSymbol#2-Bold12
    }{}
  DeclareSymbolFont{MnSy#2#3}{U}{MnSymbol#2#3}{m}{n}
  SetSymbolFont{MnSy#2#3}{bold}{U}{MnSymbol#2#3}{b}{n}
}
endgroup
newcommand{ImportFromMnSymbol}[3][1]{ImportFromMnSymbolaux{#1}{#2}{#3}}

newcommandDeclareMnSymbol[4]{DeclareMathSymbol{#1}{#2}{MnSy#3}{#4}}
newcommandDeclareMnAccent[4]{DeclareMathAccent{#1}{#2}{MnSy#3}{#4}}

ImportFromMnSymbol[0.7]{C}{70}
DeclareMnAccent{acctriangleup}{mathord}{C70}{73}

begin{document}

$acctriangleup{f}+tilde{f}$

{boldmath$acctriangleup{f}+tilde{f}$}

end{document}

enter image description here

Correct answer by egreg on February 25, 2021

So, the accents package can be used and it provides a nice method for placing that smalltringleup-Symbol from my question obove the letter. The two things one needs to bear in mind are:

  1. bm{...} must be the outermost command enclosing tilde, etc.
  2. There must be an extra set of braces inside bm if an accent is used: bm{{tilde{...}}} (Otherwise the compiling process will fail)

With usepackage{accents}:

    $
          tilde{f}
    quad tilde{bm{f}}
    quad bm{{tilde{f}}}
    quad accentset{smalltriangleup}{f}
    quad accentset{smalltriangleup}{bm{f}}
    quad bm{{accentset{smalltriangleup}{f}}}
    $

with accents package

As campa pointed out in a comment below my question, one of the two remaining issues can be fixed with the single option, i.e., usepackage[single]{accents}:

with single option

Answered by Bastian on February 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP