我正在使用pandoc
来处理 markdown 文件,这导致我在内联代码块方面出现一些问题。在某些情况下,我需要能够让一个没有任何标点符号或空格的长字符串保持在一起(不跨行拆分),而在其他情况下,我有一个没有任何空格的长字符串,但它确实有标点符号,例如逗号 ( ,
),可以自动换行。问题是我需要能够检测何时会发生过满情况并允许字符串在逗号(或其他字符)处自动换行。这是一个 MWE(在 LaTeX 中),它展示了
\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}
我希望能够测试是否会发生过满情况并允许在任意逗号处拆分字符串。
答案1
至少使用 Rmarkdown,您可以在逗号后关闭代码块,然后\hspace{0pt}
再次插入并打开代码块。这样只会在需要时产生换行符。使用 ,\linebreak[1]
您将获得相同的效果,但如果有多个逗号,您可以建议更好的换行点,方法是将 增加到[1]
或[2]
([3]
也有[4]
,但 then 不是“建议”换行,而是强制命令)。
对于不太具侵入性的解决方案,您可以将命令分配给一个字符¶
(或其他一些您永远不会在文本中使用的键盘可访问的奇怪字符)。
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`.
答案2
好的,我决定自己回答这个问题,因为我终于想出了如何让它按照我需要的方式工作。这些字符串没有空格,可以在字符串中的任何逗号处断开。因此,我将\RenewDocumentCommand\hl{m}
以下内容更改为:
\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
我把这件事弄得太复杂了。我发布这篇文章是因为我认为它可能对其他人有帮助。