4

My software automatically creates LaTeX documents. I need to display some text that I have no control over (it will be provided by the user), so it can contain problematic characters (like _ or \ or $ etc.) that would break compilation.

I could look for each of these special characters and escape them all before inserting them into the LaTeX document but I'd need an exhaustive list and it wouldn't work for anything (for instance a backslash would need to be translated into \textbackslash). So, isn't there a LaTeX solution?

I thought maybe verbatim would help, but I am not sure. I need to put this "protected" text inline (so, using \begin{verbatim}...\end{verbatim} does not do the job), and with the same font as the text around.

I thought using fancyvrb's features like this: \Verb[fontfamily=Ubuntu]|...| would do the trick, but it doesn't.

For instance, I get this: sample output

using following code (compiled with lualatex):

\RequirePackage{luatex85}
\documentclass[a4paper, fleqn, 12pt]{article}

\usepackage{lxfonts}
\usepackage{amsmath}
\usepackage{fancyvrb}

\usepackage[no-math]{fontspec}

\AtEndPreamble{\setmainfont{Ubuntu}[NFSSFamily=fontid]}

\DeclareSymbolFont{mynumbers}      {TU}{fontid}{m}{n}
\SetSymbolFont    {mynumbers}{bold}{TU}{fontid}{bx}{n}

\AtBeginDocument{
\DeclareMathSymbol{0}{\mathalpha}{mynumbers}{`0}
\DeclareMathSymbol{1}{\mathalpha}{mynumbers}{`1}
\DeclareMathSymbol{2}{\mathalpha}{mynumbers}{`2}
\DeclareMathSymbol{3}{\mathalpha}{mynumbers}{`3}
\DeclareMathSymbol{4}{\mathalpha}{mynumbers}{`4}
\DeclareMathSymbol{5}{\mathalpha}{mynumbers}{`5}
\DeclareMathSymbol{6}{\mathalpha}{mynumbers}{`6}
\DeclareMathSymbol{7}{\mathalpha}{mynumbers}{`7}
\DeclareMathSymbol{8}{\mathalpha}{mynumbers}{`8}
\DeclareMathSymbol{9}{\mathalpha}{mynumbers}{`9}
\DeclareMathSymbol{.}{\mathalpha}{mynumbers}{`.}
\DeclareMathSymbol{,}{\mathalpha}{mynumbers}{`,}
}

\newfontfamily\configfont{Ubuntu}

\usepackage{geometry}
\geometry{hmargin=0.75cm, vmargin=0.75cm}

\begin{document}
Normal text using Ubuntu font\hfill \Verb[fontfamily=Ubuntu]|Short but uncontrolled text with possible $ _ \ é è à|
\end{document}
2
  • 2
    Note that \Verb|| will not work properly if your user-provided text contains a | character.
    – Marijn
    Mar 19, 2022 at 15:40
  • @Marijn that's true and I don't see any way to avoid it. Well, apart from detecting and removing possible | before insertion in the LaTeX document. It would be a forbidden character for the user, I think that's a minor annoyance though.
    – zezollo
    Mar 20, 2022 at 9:24

2 Answers 2

4

The fontfamily key in \Verb accepts a NFSS family name. In your case that's fontid, not 'Ubuntu(specified by theNFSSFamilykey to\setmainfont`). Then you get

\RequirePackage{luatex85}
\documentclass[a4paper, fleqn, 12pt]{article}

\usepackage{lxfonts}
\usepackage{amsmath}
\usepackage{fancyvrb}

\usepackage[no-math]{fontspec}

\AtEndPreamble{\setmainfont{Ubuntu}[NFSSFamily=fontid]}

\DeclareSymbolFont{mynumbers}      {TU}{fontid}{m}{n}
\SetSymbolFont    {mynumbers}{bold}{TU}{fontid}{bx}{n}

\AtBeginDocument{
\DeclareMathSymbol{0}{\mathalpha}{mynumbers}{`0}
\DeclareMathSymbol{1}{\mathalpha}{mynumbers}{`1}
\DeclareMathSymbol{2}{\mathalpha}{mynumbers}{`2}
\DeclareMathSymbol{3}{\mathalpha}{mynumbers}{`3}
\DeclareMathSymbol{4}{\mathalpha}{mynumbers}{`4}
\DeclareMathSymbol{5}{\mathalpha}{mynumbers}{`5}
\DeclareMathSymbol{6}{\mathalpha}{mynumbers}{`6}
\DeclareMathSymbol{7}{\mathalpha}{mynumbers}{`7}
\DeclareMathSymbol{8}{\mathalpha}{mynumbers}{`8}
\DeclareMathSymbol{9}{\mathalpha}{mynumbers}{`9}
\DeclareMathSymbol{.}{\mathalpha}{mynumbers}{`.}
\DeclareMathSymbol{,}{\mathalpha}{mynumbers}{`,}
}

\newfontfamily\configfont{Ubuntu}

\usepackage{geometry}
\geometry{hmargin=0.75cm, vmargin=0.75cm}

\begin{document}
Normal text using Ubuntu font\hfill \Verb[fontfamily=fontid]|Short but uncontrolled text with possible $ _ \ é è à|
\end{document}

enter image description here

3

fontid is used in the manual just as a signpost, you should use a meaningful name.

Here are some more suggestions to just use lxfonts only for math and to avoid it changing some important defaults for text mode.

\documentclass[a4paper, fleqn, 12pt]{article}
\usepackage{geometry}
\usepackage{amsmath}
\usepackage{fancyvrb}
\usepackage{lxfonts}
\usepackage[no-math]{fontspec}

\geometry{hmargin=0.75cm, vmargin=0.75cm}

%%% fixes in order to use lxfonts only for math
\let\savedrmdefault\rmdefault
\let\savedsfdefault\sfdefault
\let\savedttdefault\itdefault
\let\saveditdefault\itdefault
\let\savedsldefault\sldefault
\let\savedbxdefault\bxdefault
\AtEndPreamble{% undo the nonmath settings by lxfonts
  \let\rmdefault\savedrmdefault
  \let\sfdefault\savedsfdefault
  \let\ttdefault\saveditdefault
  \let\itdefault\saveditdefault
  \let\sldefault\savedsldefault
  \let\bxdefault\savedbxdefault
  \setmainfont{Ubuntu}[NFSSFamily=ubuntu]%
}
%%%

\DeclareSymbolFont{mynumbers}{TU}{ubuntu}{m}{n}
\SetSymbolFont{mynumbers}{bold}{TU}{ubuntu}{bx}{n}

\AtBeginDocument{%
  \DeclareMathSymbol{0}{\mathalpha}{mynumbers}{`0}%
  \DeclareMathSymbol{1}{\mathalpha}{mynumbers}{`1}%
  \DeclareMathSymbol{2}{\mathalpha}{mynumbers}{`2}%
  \DeclareMathSymbol{3}{\mathalpha}{mynumbers}{`3}%
  \DeclareMathSymbol{4}{\mathalpha}{mynumbers}{`4}%
  \DeclareMathSymbol{5}{\mathalpha}{mynumbers}{`5}%
  \DeclareMathSymbol{6}{\mathalpha}{mynumbers}{`6}%
  \DeclareMathSymbol{7}{\mathalpha}{mynumbers}{`7}%
  \DeclareMathSymbol{8}{\mathalpha}{mynumbers}{`8}%
  \DeclareMathSymbol{9}{\mathalpha}{mynumbers}{`9}%
  \DeclareMathSymbol{.}{\mathord}{mynumbers}{`.}%
  \DeclareMathSymbol{,}{\mathpunct}{mynumbers}{`,}%
}

\newfontfamily\configfont{Ubuntu}

\newcommand{\uverb}{\Verb[fontfamily=ubuntu]}

\begin{document}

Normal text using Ubuntu font

\uverb|Short but uncontrolled text with possible $ _ \ é è à|

$(a,b+123)$ 123

\end{document}

enter image description here

Note that the period should be \mathord and the comma \mathpunct.

You must log in to answer this question.

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