Inline verbatim text keeping the same font as normal text

fancyvrbfontsluatexverbatim

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}

Best Answer

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