How can I put a symbol (~) above a letter?
Show older comments
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?
Accepted Answer
More Answers (1)
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.

The decimal code for the combining tilde (~) is 771. To add a tilde to the capital letter A,
dc = [double('A'),771]
ch = char(dc)
Noteworthy
- 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.
- Text rotation results in misalignment. If text rotation is not 0° the combination will no longer align (e.g. y-axis labels).
- 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]);
box(ax,'on')
hold(ax,'on')
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]};
legend(h,cellfun(@char,legStr,'UniformOutput',false),...
'FontSize',16, 'Location','BestOutside')
% Axis labels
xlabel(ax,char([double('X'),770,845]),'FontSize',20)
ylabel(ax,char([double('Y'),770]),'FontSize',20,...
'Rotation',0,'VerticalAlignment','middle','HorizontalAlignment','right')
% Crazy diacritics title
rng('default')
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]))
Here are some relevant ASCII resources. Happy hunting.
2 Comments
Alexander Marek
on 27 Apr 2021
This works great! Only for the export to vector graphics e.g. EPS or PDF this doesn't work for me (Matlab 2020a). In the EPS/PDF the tilde always appears behind the target character.
Adam Danz
on 27 Apr 2021
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.
Categories
Find more on Software Development in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!