3

I'm trying to add background color to \texttt text, and defining a command for that like this: \newcommand{\code}[1]{\texttt{\hl{#1}}} . Sometimes those lines span more than a few words (with or without spaces), and I use \allowbreak to make it break without hyphenation on some places (somewhere it's before punctuation such as ., somewhere it's after space, somewhere it's before capital letter). Without \hl, it worked like expected.

However, when I put \hl, first it complains about I came across hyphenatable material enclosed in group braces, which I can't handle. Either drop the braces or make the material unbreakable using an \mbox (\hbox). on a few places where I do have spaces, followed by \allowbreak (e.g. \code{CREATE \allowbreak TABLE}). Second, I've noticed it broke text before \allowbreak instead of after on a few places and dropped last character or five, without reporting any error. It also seems to struggle with coloring text after breaking on a few places, so highlight spans into the right margin, but skips beginning of the next line.

I'm using soulutf8 if it makes any difference, and highlighted text is sometimes in Cyrillic, sometimes in Latin, sometimes in both.

Am I using the right tool for the job? If so, how to set background color on tt text with custom break points?

Here's the MWE for some of the issues, with (unexpected) result in comment after each paragraph. I'm not including \allowbreak after space part, because as pointed out it's practically useless (though I'd expect it to be ignored, and not throw an error):

\documentclass[11pt]{book}

\usepackage[serbianc]{babel}
\usepackage{xcolor, soulutf8}
\usepackage{vmargin}

\begin{document}

\newcommand{\code}[1]{\texttt{\hl{#1}}}

\setmargrb{0.8in}{1in}{1.6in}{1.6in} 

Омотач за податке прикупљене пре 2017. године је дефинисан класом
\code{rs.lukaj\allowbreak.upisstats\allowbreak.scraper\allowbreak.obrada\allowbreak.UcenikWrapper}, 
унутар које се налазе класе. %highlight goes over margin and cut off

Омотач за податке прикупљене 2017. године је дефинисан класом
\code{rs\allowbreak.lukaj\allowbreak.upisstats\allowbreak.scraper\allowbreak.obrada2017\allowbreak.UcenikW}, 
и за разлику од %text broke in the middle and cut off

Унутар \code{conf/} директоријума се налазе конфигурацијски фајлови. 
Најважнији део \code{conf/\allowbreak application\allowbreak.conf} 
фајла су параметри % text cut off even when not broken

ће се метода позвати за сваку путању. Нпр. ако корисник посети 
\code{\{адреса\_сервера\}\allowbreak/query\allowbreak?initial=asdf}, 
позваће се % text broken before allowbreak and cut off
\end{document}

And as a result I'm getting this

enter image description here

3
  • Welcome to TeX.SX! You should provide a compilable minimal working example to your problem in order for others to replicate your problem. This way, it is much easier for others to help you! Feb 7, 2018 at 22:42
  • Please provide a test example, but CREATE \allowbreak TABLE is rather strange input as line breaking is allowed anyway at the space. Feb 7, 2018 at 22:48
  • @DavidCarlisle you're right, I've updated the question with some other examples where it breaks (unexpectedly)
    – Luke
    Feb 7, 2018 at 23:07

1 Answer 1

2

Use \soulregister:

\documentclass[11pt]{book}

\usepackage[serbianc]{babel}
\usepackage{xcolor, soulutf8}
\soulregister{\allowbreak}{0}

\newcommand{\code}[1]{\texttt{\hl{#1}}}

\begin{document}

Омотач за податке прикупљене пре 2017. године је дефинисан класом
\code{rs.lukaj\allowbreak.upisstats\allowbreak.scraper\allowbreak.obrada\allowbreak.UcenikWrapper}, 
унутар које се налазе класе. %highlight goes over margin

Омотач за податке прикупљене 2017. године је дефинисан класом
\code{rs\allowbreak.lukaj\allowbreak.upisstats\allowbreak.scraper\allowbreak.obrada2017\allowbreak.UcenikW}, 
и за разлику од %text broke in the middle and cut off

Унутар \code{conf/} директоријума се налазе конфигурацијски фајлови. 
Најважнији део \code{conf/\allowbreak application\allowbreak.conf} 
фајла су параметри % text cut off even when not broken

ће се метода позвати за сваку путању. Нпр. ако корисник посети 
\code{\{адреса\_сервера\}\allowbreak/query\allowbreak?initial=asdf}, 
позваће се % text broken before allowbreak and cut off
\end{document}

enter image description here

A note. Don't use vmargin. It doesn't cooperate with several other packages, notably TikZ.

3
  • 1
    This does indeed fix everything, thanks! Any explanation (or useful resource/link to) how it works and why it behaved so erratically without it? As for vmargin, the template I used came with it, so I just went with it, but I did notice it doesn't behave well often. Time to change it then.
    – Luke
    Feb 7, 2018 at 23:19
  • @Luke The \soulregister command is described in the documentation for soul.
    – egreg
    Feb 7, 2018 at 23:25
  • From the documentation of the package: If a token is a command registered via \soulregister, it is executed immediately. Other tokens are only counted and trigger some action when a certain number is reached (quotes and dashes). [...] To convince soul not to feed font switching (or other) commands to the analyzer, but rather to execute them immediately, they have to be registered [...]. Nov 9, 2022 at 16:49

You must log in to answer this question.

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