2

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

\documentclass[12pt,]{article}
\usepackage{lmodern}
\usepackage{mathptmx} % makes Time New Roman the default font
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
% use upquote if available, for straight quotes in vebatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
% use microtype if available
\IfFileExists{microtype.sty}{%
  \usepackage[]{microtype}
  \UseMicrotypeSet[protrusion,expansion]{basicmath} % disable protrusion for tt fonts
}{}
\usepackage[margin=1in]{geometry}
\usepackage{listings}
\setlength{\emergencystretch}{3em} % prevent overfull lines

\usepackage{parskip}
\usepackage{xcolor}
\usepackage{soul}

\definecolor{codegray}{HTML}{F0F0F0}
\definecolor{framegray}{HTML}{C0C0C0}
\sethlcolor{graycode}

% unbreak escaped space character in inline codeblocks
\usepackage{xparse}
\usepackage{ragged2e}

\let\hlORIG\hl

\ExplSyntaxOn
\tl_new:N \l_jdhao_hlx_tl
\RenewDocumentCommand\hl{m}{%
  \tl_set:Nn\l_jdhao_hlx_tl{#1}%
  \tl_replace_all:Nnn\l_jdhao_hlx_tl{\ }{~}%
  \tl_replace_all:Nnn\l_jdhao_hlx_tl{-\/-}{\mbox{-\/-}}% added
  \exp_args:NV\hlORIG\l_jdhao_hlx_tl}%
\ExplSyntaxOff

% 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.
\let\oldTexttt\texttt
\renewcommand{\texttt}[1]{%
  \begingroup
  \hyphenpenalty=10000
  \exhyphenpenalty=10000
  \lefthyphenmin=16
  \setlength\emergencystretch{\hsize}\hbadness=10000
  \sethlcolor{codegray}{\ttfamily\hl{#1}}%
  \endgroup
}
\RaggedRight
\begin{document}
\section{Example}\label{example}

\begin{itemize}
\item
  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{[}127.0.0.1:8888{]},beta"}.
\end{itemize}
\end{document}

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
1

mwe

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:

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

- string does not contain any spaces and normally we don't want to break:
`aaaaaa,aaaaaa,aaaaaa,aaaaaa,aaaaaa,aaaaaa,aaaaaa,aaaaaa,aaaaaa,aaaaaa,aaaaa`.

- string does not contain any spaces and normally we don't  want to break:
`aaaaa,`¶`aaaaa,`¶`aaaaa,`¶`aaaaa,`¶`aaaaa,`¶`aaaaa,`¶`aaaaa,`¶`aaaaa,`¶`aaaaa,`¶`aaaaa`.
  • 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
0

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:

\let\hlORIG\hl
\ExplSyntaxOn
\tl_new:N \l_jdhao_hlx_tl
\RenewDocumentCommand\hl{m}{%
  \tl_set:Nn\l_jdhao_hlx_tl{#1}%
  \tl_replace_all:Nnn\l_jdhao_hlx_tl{\ }{~}%
  \tl_replace_all:Nnn\l_jdhao_hlx_tl{-\/-}{\mbox{-\/-}}%
  \tl_replace_all:Nnn\l_jdhao_hlx_tl{,}{,{\-}}% allow line breaking at a comma
  \exp_args:NV\hlORIG\l_jdhao_hlx_tl}%
\ExplSyntaxOff

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.