2

I'm using soul's \texthl in combination with \texttt to render inline code that can break at arbitrary hyphenation points when it reaches the end of a line. To simulate the "verbatim" aspect of inline code I'm basically escaping any special characters (\${}_&%#~^) that may appear within the verbatim text.

Here's an example of what I was able to achieve:

\documentclass{article}

\usepackage[margin=2cm]{geometry}
\usepackage{xcolor}
\usepackage{fontspec}
\usepackage{soul}

\definecolor{bg}{rgb}{0.85, 0.85, 0.85}
\setmonofont{Courier New}
\sethlcolor{bg}

\begin{document}

     aklsdklasj asdkj alskjd aslkj dalskjd asldjd asad ldasd  sd pjwed e jope pojep 
     ojepoj ejpm pwj slakdj as  \texthl{\texttt{\textbackslash \-\-n\-e\-w\-c\-o\-m\-m\-a\-n\-d\-\{\-t\-e\-s\-t\-\}\-\{\-o\-u\-t\-\}}}
\end{document}

Which compiles as follows using latexmk -lualatex test.tex:

enter image description here

Unfortunately, if I try to render x = 3 the highlighting doesn't work anymore:

\documentclass{article}

\usepackage[margin=2cm]{geometry}
\usepackage{xcolor}
\usepackage{fontspec}
\usepackage{soul}

\definecolor{bg}{rgb}{0.85, 0.85, 0.85}
\setmonofont{Courier New}
\sethlcolor{bg}

\begin{document}

     aklsdklasj asdkj alskjd aslkj dalskjd asldjd asad ldasd  sd pjwed e jope pojep 
     ojepoj ejpm pwj slakdj as  \texthl{\texttt{\textbackslash \-\-n\-e\-w\-c\-o\-m\-m\-a\-n\-d\-\{\-t\-e\-s\-t\-\}\-\{\-o\-u\-t\-\}}}

    \texthl{\texttt{x\- \-=\- \-3}}
\end{document}

enter image description here

Does anyone know why?

1
  • 2
    As you use lualatex: drop soul and use lua-ul instead. That is much less fragile. Sep 3, 2022 at 8:08

1 Answer 1

3

Folowing up and expanding on @UlrikeFischer's suggestion, you may want to start using the lua-ul package instead of soul. I would further like to suggest that you load the xurl package and enclose the longish strings in \path wrappers to allow line breaks at arbitrary locations. That way, you can write

\highLight{\path|\newcommand{\test}{out}|}

instead of

\texthl{\texttt{\textbackslash \-\-n\-e\-w\-c\-o\-m\-m\-a\-n\-d\-\{\-\textbackslash\-t\-e\-s\-t\-\}\-\{\-o\-u\-t\-\}}}

enter image description here

\documentclass{article}

\usepackage{fontspec}
\setmonofont{Courier New}

\usepackage{luacolor} % repalcement for 'xcolor'
\definecolor{bg}{rgb}{0.85, 0.85, 0.85}

\usepackage{lua-ul}  % replacement for 'soul' package
\LuaULSetHighLightColor{bg}

\usepackage{xurl} % for \path macro, allow line breaks at arbitrary locations

\begin{document}
aklsdklasj asdkj alskjd aslkj dalskjd asldjd asadcd   
pwj slakdj as \highLight{\path|\newcommand{\test}{out}|}

\smallskip
\highLight{\path+x=3+}
\end{document}
2
  • This seems to work very well, though I think that I prefer to go with patching mintinline. That's a lovely suggestion to use \path from xurl. Any idea why this macro isn't listed in the xurl documentation?
    – user32882
    Sep 5, 2022 at 3:29
  • @user32882 - Why-type questions are best posed to the authors of the piece of software in question. I can only guess that the because xurl is "just" [!] a front-end to the url package, the author of the xurl package didn't want to risk coming across as stealing anyone else's intellectual contributions. That said, I don't think it would have hurt to provide a sentence such as "Because thexurl package loads the url package, all of the macros defined by the url package, including \path and \urldef, are also available."
    – Mico
    Sep 5, 2022 at 4:52

You must log in to answer this question.

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