I am using pandoc to process an markdown file that is causing me some issues with regard to in-line code blocks. In some cases I need to be able to have a long string without any punctuation or spaces remain together (not split across lines), and in other cases I have a long string without any spaces, but it does have punctuation, such as a comma (,), where it can be word wrapped. The problem is that I need to be able to detect when a overfull condition is going to occur and allow the string to be word wrapped at the comma (or other character). Here is an MWE (in LaTeX) that exhibits the

\usepackage{mathptmx} % makes Time New Roman the default font
% use upquote if available, for straight quotes in vebatim environments
% use microtype if available
  \UseMicrotypeSet[protrusion,expansion]{basicmath} % disable protrusion for tt fonts
\setlength{\emergencystretch}{3em} % prevent overfull lines



% unbreak escaped space character in inline codeblocks


\tl_new:N \l_jdhao_hlx_tl
  \tl_replace_all:Nnn\l_jdhao_hlx_tl{\ }{~}%
  \tl_replace_all:Nnn\l_jdhao_hlx_tl{-\/-}{\mbox{-\/-}}% added

% override the inline coding style to show gray highlight box
% I needed to have the penalties, stretch and minimum because 
% had the string FILENAME in an in-line code block that kept
% getting broken up as FILE at the end of one line and NAME at
% the start of the next, that needed to be kept together.

  string does not contain any spaces and normally we don't wany to break
  under normal conditions, but when the \texttt{\textbackslash{}hbox}
  gets overfull by a bunch, \texttt{"ab,alpha{[}{]},beta"}.

I'd like to be able to test if an overfull condition is about to occur and allow the string to be split at any of the commas.

  • One idea I have is to replace the commas with a character that will allow the text to be wrapped, but to not print that character to the output files. Is this possible? – JonBelanger Dec 28 '18 at 23:11
  • I think I'm close. I added \tl_replace_all:Nnn\l_jdhao_hlx_tl{,}{\allowbreak ,} to the \RenewDocumentCommand\hl macro. This caused the line to break after the ] and not the comma. The line broke, but the gray box around it did not. I tried \tl_replace_all:Nnn\l_jdhao_hlx_tl{,}{,\allowbreak}. This caise the line to break before the ] and not the comma. This time the gray box around the text went with the text. If I can get the line to break with the comma on the first line, and the remaining string on the second line, and the gray box to go with the text, perfect. – JonBelanger Dec 28 '18 at 23:33
  • What about simply left a space after the comma? Maybe is not allowed in your code, but generally is allowed and increases the legibility also when the code is not wrapped. – Fran Dec 29 '18 at 4:33
  • The statement in the list item indicates that the string is not to contain any spaces, so adding a space will cause issues. Also, the markdown file that generates this is also used for HTML, and that does not have an issue. Just the LaTeX to PDF. – JonBelanger Dec 29 '18 at 14:39


At least using Rmarkdown you can close the code block after the coma, insert \hspace{0pt} and open the code block again. That will produce a line break only when it is needed. With a \linebreak[1] you will obtain the same effect, but if there are several commas, you can suggest the better break point increasing the [1] to [2] or [3] (there are also [4], but then is not a "suggest" to a break the line, but a mandatory order).

For a less intrusive solution, you can assign the command to a character as (or some other odd character accessible by keyboard that you will never use in text).

The code for the screen shot in Rmarkdown:

    - \usepackage{newunicodechar}
    - \newunicodechar{¶}{\hspace{0pt}}
  pdf_document: default

- string does not contain any spaces and normally we don't want to break:

- string does not contain any spaces and normally we don't  want to break:
  • Unfortunately, this did not work with pandoc. It does the same thing that the \allowbreak did (it behaved identically). – JonBelanger Dec 31 '18 at 13:49

OK, I decided to answer my own question, as I finally figured out how to get this to work the way I needed it to. These strings, which have not spaces, can be broken at any commas within the string. So, I changed the \RenewDocumentCommand\hl{m} to the following:

\tl_new:N \l_jdhao_hlx_tl
  \tl_replace_all:Nnn\l_jdhao_hlx_tl{\ }{~}%
  \tl_replace_all:Nnn\l_jdhao_hlx_tl{,}{,{\-}}% allow line breaking at a comma

I was making this far more difficult than it needed to be. I'm posting this because I think it may be helpful to others.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.