MATLAB Answers

How can I put a symbol (~) above a letter?

699 views (last 30 days)
Veronica Carrillo
Veronica Carrillo on 17 Jul 2018
Commented: Adam Danz on 27 Apr 2021 at 11:59
I need to put ~ avobe a letter. Is this possible in matlab? I have tried some things but the symbol appears before and after the letter but not above.
[Moved from section for answers] I need to put it in many letters.
  1 Comment
Dennis on 17 Jul 2018
I don't know if there is a way to put it above any letter, char(195) and char(209) work for A and N. You need a specific letter?

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 17 Jul 2018
Where do you need to add the tilde above the letter?
In a variable name? That's not going to work.
In the title or axis label of an axes? Set the object's Interpreter property to LaTeX and use the \tilde command.
text(0.5, 0.5, '$$\tilde{a}$$', 'Interpreter', 'LaTeX')
Somewhere else? Tell us where.
Sim on 26 Oct 2020
title('$\tilde{A} = \tilde{A}+E$', 'Interpreter','latex')
You can also adjust size and font:
title('$\tilde{A} = \tilde{A}+E$', 'Interpreter','latex','FontWeight','bold','FontSize',20)

Sign in to comment.

More Answers (1)

Adam Danz
Adam Danz on 17 Apr 2021 at 22:36
Edited: Adam Danz on 17 Apr 2021 at 22:51
An alternative to using LaTeX to represent a diacritical character in a text object is to use a combining character to join a glyph and a diacritic (e.g. ë æ ñ ø å č).
How to combine a character and a diacritic?
To create a diacrtitcal character combination, find the combining character for the chosen diacritic and append it to the chosen character.
Wiki provides a well organized table of combining characters. Here are the first four out of 112!
The decimal code for the combining tilde (~) is 771. To add a tilde to the capital letter A,
dc = [double('A'),771]
dc = 1×2
65 771
ch = char(dc)
ch = 'Ã'
  1. Not all fonts support the combinations. For example, the default font in the Matlab editor and command window is Monospaced which produces in a 1x2 character vector instead of à (unicode FAQ). Don't ask me why it appears correctly when produced above within the forum; it might be due to a different UTF bit format.
  2. Text rotation results in misalignment. If text rotation is not 0° the combination will no longer align (e.g. y-axis labels).
  3. Multiple combining characters is supported. For example, to add an overline above and a plus sign below the letter O, char([double('O'),773,799])
Monospace font:
Display an assortment of diacritic-character combinations in text objects
characters = ['A','e','o','T','Z','Q','m'];
diacriticCombineChars = [768 771 773 776 778 780 770];
% Grave accent, Tilde, Overline, Diaeresis, Ring above, caron, circumflex
% Create all combinations of letters and diacritics
[ch,di] = meshgrid(double(characters), diacriticCombineChars);
diaChars = cellstr(char([ch(:),di(:)]));
% Set character coordinates
[x,y] = meshgrid(1:numel(characters), 1:numel(diacriticCombineChars));
% Plot text
fig = figure();
ax = axes(fig,'xlim',[0,max(x(:))+1],'YLim',[0,max(y(:))+1]);
text(ax, x(:),y(:),diaChars,'HorizontalAlignment','Center','FontSize',20)
% Add dummy objects to populate legend
h = plot(ax, nan(2,5),'o');
legStr = {[double('A'),771], [double('V'),775,'(x)']...
[double('t'),778], [double('x'),844],[double('a'),831,double('c'),831]};
'FontSize',16, 'Location','BestOutside')
% Axis labels
% Crazy diacritics title
base = double('Diacritics');
dcodes = 768:879;
ri = randi(numel(dcodes),[5,numel(base)]);
dv = [base;dcodes(ri);32*ones(size(base))];
tlh = title(ax,char(dv(:)'),'FontSize',30,'FontWeight','normal');
ax.Position(4) = ax.Position(4)*.7;
tlh.Position(2) = tlh.Position(2)*1.1;
ASCII diacritic combinations
Some ASCII characters contain diacritics which provides a shortcut. It's just a matter of finding the character codes. For example,
num2cell(char([202 195 214 248 229]))
ans = 1×5 cell array
{'Ê'} {'Ã'} {'Ö'} {'ø'} {'å'}
Here are some relevant ASCII resources. Happy hunting.
Adam Danz
Adam Danz on 27 Apr 2021 at 11:59
Bummer. Thanks for the info Alexander. I just tried exportgraphics to export the figure in my answer to PDF in Matlab Online (r2021a) and the diacritic characters were not preserved. When using the vector contentType, exportgraphics is supposed to include embeddable fonts so I wonder what the problem is.
However, when I use copygraphics(fig,'ContentType','vector'), and paste the figure into a document then save as PDF, all characters are preserved and there isn't a problem.
Also, when I select the figure's menu bar > Edit > copy figure, or if I select copy as vector graphics from the axes' toolbar, then paste into a document and save as PDF, all characters are preserved without any problems.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!