1

Im using the soul package for highlighting. I noticed that \hl will go simply out of the margin, if the word is too long:

\hl{Loremipsumdolorsitamet,consetetursadipscingelitr,seddiamnonumyeirmodtemporinviduntutlaboreetdoloremagnaaliquyamerat,seddiamvoluptua.Atveroeosetaccusametjustoduodoloresetearebum.Stetclitakasdgubergren,noseatakimatasanctusestLoremipsumdolorsitamet}

How can I make it automatically line-break?

  • 1
    Welcome to TeX.SX! It will indeed behave like this if you use words that are not part of the languange you set up for the document, because LaTeX won't know how to hyphenate such a string. If you use sensible words, this won't happen. If you really need to type such a string, you can tell LaTeX to hyphenate at a certain point by inserting \-. – Jasper Habicht Nov 18 '22 at 15:54
  • 1
    Thanks for the answers! In the referenced post (https://tex.stackexchange.com/questions/100391/how-to-wrap-a-word-in-line-without-hyphen) the location of the line break must be manually specified. However, I am looking for a solution/ environment that puts a line break automatically as the text would go out of the margin. – user16910689 Nov 18 '22 at 17:02
  • Maybe this: https://tex.stackexchange.com/q/6171/47927 ? But seqsplit might not work together with the soul package ... – Jasper Habicht Nov 18 '22 at 21:09
  • Actually, I think this should not be closed, since the OP wants a solution that automatically breaks the line without hyphenation which is additionally compatible with the commands provided by the soul package. – Jasper Habicht Nov 18 '22 at 21:21

1 Answers1

0

From your comments, I understand that you want to automatically break long arbitrary strings at the end of the line without hyphenation and additionally these strings should be placed inside a \hl command as provided by the soul package.

Actually, automatically break long arbitrary strings at the end of the line without hyphenation is not a big problem and has been nicely shown in this nice answer, which you could use as starting point. However, since the macros provided by the soul package do complicated things with the token lists they are fed with, we need to approach this with some caution.

The following code creates a macro \hlautosplit that breaks a string automatically at the line end without hyphenation and wraps everything in an \hl command.

\documentclass{article}
\usepackage{soul}

\ExplSyntaxOn \tl_new:N \l_hlautosplit_hlsplitstring_tl \NewDocumentCommand{\hlautosplit} { m } { \tl_clear:N \l_hlautosplit_hlsplitstring_tl \str_map_inline:nn { #1 } { \tl_put_right:Nn \l_hlautosplit_hlsplitstring_tl { ##1 \soulomit{\hspace{0pt plus 0.1pt}} } } \exp_args:No \hl { \l_hlautosplit_hlsplitstring_tl } } \ExplSyntaxOff

\begin{document}

\hlautosplit{Loremipsumdolorsitamet,consetetursadipscingelitr,seddiamnonumyeirmodtemporinviduntutlaboreetdoloremagnaaliquyamerat,seddiamvoluptua.Atveroeosetaccusametjustoduodoloresetearebum.Stetclitakasdgubergren,noseatakimatasanctusestLoremipsumdolorsitamet}

\end{document}

enter image description here

What is important here is that you cannot place any \discretionary hyphenation into the argument of \hl or into any other of the macros provided by the soul package. But we don't need this here: Instead we just add a tiny space after each letter which allows TeX to break the string at any character. However, we need to mask this tiny space using \soulomit since we don't want to bother the soul mechanism with this (and it would complain if we would not mask it). Finally, we need to expand the whole token list we just created before soul sees it inside the \hl macro.

Result with \sethlcolor{yellow}:

enter image description here

  • Thanks so much for this answer. I noticed that spaces are getting automatically removed here. I know that adding a "~" causes an unbreakable space, however, I was wondering how one would do this here automatically? – user16910689 Dec 02 '22 at 13:04
  • @user16910689 Well, I thought that you would need this to place stuff that has no spaces. If there are spaces in the string, line breaking will take place there and there is no need for such a macro in my opinion. Maybe clarify what exactly you're after. I fear, I don't quite understand the use case here. Actually, you can just replace \tl_map_inline:nn by \str_map_inline:nn. Spaces should be kept then. – Jasper Habicht Dec 02 '22 at 13:05
  • I changed the code. Spaces should now be kept in the output. – Jasper Habicht Dec 02 '22 at 13:12
  • 1
    This works perfectly. I was trying to highlight code fragments within the minted environment , and without the macro it would go out of the margin. Thanks again! – user16910689 Dec 02 '22 at 13:38