2

This is edit. See original question below.

In math mode I would like to use identifiers that contain of whole word. That word is not in English and does contain letters with diacritic marks. Example of such word is kľúč.

I have tried to use commands like \mathit{kľúč} or \mathnormal{kľúč} inside math environment. They work well when there are no diacritic marks for example \mathit{kluc} renders the word together with no ugly spaces between letters. When diacritic marks are present the letters are rendered with spaces for example command \mathit{kľúč} inside math environment produces something like k ľ úč instead of klúč.

How can I typeset multi-letter identifiers that contain diacritic marks inside math environment?


Original question text:

The title says it. I want to use letters like ľ, ú, š, ô etc. as part of mathematical identifier (\mathit) in math environment. However unlike standard english letters they are rendered with unpleasant spaces before and after them. How do I solve this?

Example of used command:

$\left\langle \mathit{kl\mkern-5mu\mathchar19\acute{u}\check{c}},\mathit{hodnota}\right\rangle $

This is LaTeX code produced by Lyx when I edit that expression using graphical editor. Expected output is something like <kľúč, hodnota> (in english this would be <key, value>) but what I get looks more like <k ľ ú č, hodnota>.

3
  • 3
    Why do you use \mathit at all? math mode tells latex that all normal letters (i.e. the ones with no mathematical significance) are variables and get bigger spacing. I doubt that's what you want. On top of that the $ already starts math mode and usually math is set italic/slanted. Are you sure you don't want \textit?
    – Max
    Dec 30, 2012 at 19:20
  • @Max No I'm not. But I need to use mathematical expressions (or pseudo-code programatic expressions) and in those I want to use multi-letter identifiers. Those multi-letter identifiers will contain also letters with diacritics marks. Al the letters in those identifiers must be rendered together with no space between them. That is what I want to do, but I do not know how.
    – Rasto
    Dec 30, 2012 at 19:33
  • @drasto You can (and should) use \textit in math mode. If you load amsmath also \textit will change size in subscripts and superscripts.
    – egreg
    Dec 30, 2012 at 19:58

3 Answers 3

4

The characters allowed in \mathit are just plain ASCII, with possibly math accents over them. For words it's better to use \textit, which will change size in subscripts and superscripts, provided amsmath is loaded.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[slovak]{babel}
\usepackage{amsmath}

\begin{document}
$\langle\textit{kľúč}, \textit{hodnota}\rangle$

$A_\textit{kľúč}$
\end{document}

enter image description here

If LyX is unable to provide \textit, then it's a problem of LyX, not of LaTeX.

7
  • I really thank you for this answer. @barbara beeton's was good but yours works with Lyx well and is very simple. Only minor point is that it does not display perfectly in Lyx editor but I can live with that. Thank you!
    – Rasto
    Dec 30, 2012 at 21:13
  • \textit uses the text font, which may be different from the math font. Then, using \textit for math identifiers would look wrong. For example, if you wrap the formulae with \textsf, the text in \textit is sans-serif, but other math text is serif. (Demonstration: \textsf{Sans-serif text with some supposedly serif math $a + b + c = \langle\textit{kľúč}, \textit{hodnota}\rangle$}) A more realistic example would be adding this to the preamble: \usepackage{fontspec} \setmainfont{Tex Gyre Termes}
    – matj1
    May 28, 2023 at 14:23
  • I think that a more reliable way would be to renew \mathit or make a new command to detect the math font, put the given text in \textit and locally change the font in the \textit to the detected math font, but I don't know how to do that. Also, I think that, if this is the best way to do it, LATEX has a serious shortcoming in this regard.
    – matj1
    May 28, 2023 at 14:32
  • @matj1 \textnormal{\itshape...} would probably do.
    – egreg
    May 28, 2023 at 16:21
  • @egreg But that doesn't work if the text font and the math font are from different typefaces (like Tex Gyre Termes and Latin Modern Roman).
    – matj1
    May 29, 2023 at 14:19
3

it looks to me as if this is really text, not math.

if you have many such constructions, i suggest creating a macro something like this to take care of them:

\newcommand{mykeyval}[2{%
  \langle\text{\textit{#1}, \textit{#2}}\rangle}

this will require \usepackage{amsmath} which defines \text. it would be used this way, and works only within a math environment:

... \mykeyval{xxx}{yyy} ...

only the angle brackets are really math; \text allows you to embed text strings in math environments. i prefer non-italic commas in such situations, which is why i exited from italic for the comma.

(an earlier version of this answer suggested a construction that is used by tugboat to enter pseudocode, but that, in its usual formulation, doesn't work properly within \text embedded in a math environment, so the suggestion has been removed here.)

3
  • Example in my question is really an example. It need to use multi-letter identifiers containing letters with dicritics marks inside complex mathematical expresions. And I prefer them to be italic as proper in mathematical formulas. I would prefer not to exit math environment and set italics every time I use such identifier because that would probably make LaTeX code one big mess.
    – Rasto
    Dec 30, 2012 at 19:47
  • Looks like it works. I defined new macro \newcommand{\mathid}[1]{\text{\textit{#1}}} and then used $\left\langle \mathid{kľúč},\mathit{hodnota}\right\rangle $. This works as expected. My problem is that now it looks like I'll have to edit all formulas in plain LaTeX because when using Lyx editor it keeps changing same expression to something like $\left\langle \mathid{kl\mkern-5mu\mathchar19\acute{u}\check{c}},\mathit{hodnota}\right\rangle $. And this does not even compile saying: I've inserted a begin-math/end-math symbol since I think you left one out. Proceed, with fingers crossed.
    – Rasto
    Dec 30, 2012 at 20:55
  • @drasto -- i've updated the answer to use the formulation you said worked, and also my comment. Dec 30, 2012 at 21:46
0

If you use XeLaTeX or LuaLaTeX and load unicode-math or a suitable font package, you can use letters with diacritics with math fonts. It does not work with the default font, so you have to specify \mathit or something other, but you do not have to use the text fonts.

\documentclass{article}
\usepackage{unicode-math} % or libertinus or other font packages
\begin{document}

\textsf{Sans-serif text with serif math $\langle\mathit{kľúč}, \mathit{hodnota}\rangle$}

\end{document}

example of serif math text with diacritics in a sans-serif context

The sans-serif text is there to show that the font setting for the surrounding text does not affect the math text, which is a problem in the accepted solution by egreg.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .