自定义宏以带或不带间距包装内容

自定义宏以带或不带间距包装内容

我的问题类似于换行没有空格的长文本内容有两个答案来包装内容,例如

\seqsplit{ATGTCCACTGATAAAAGTACACGCTATAATTTTCAGATTGAGAAAGCCCCCTCGTTGGCGTACGCTGCAGGTCGAC}

或者上面答案中提到的自定义宏

\def\enablehyph#1{\enablehyphA #1\end}
\def\enablehyphA#1{\ifx\end#1\unskip\unpenalty
   \else#1\penalty0\hskip0pt plus1pt\relax
   \expandafter\enablehyphA\fi}

\enablehyph{dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd}

这些方法可以工作,并且它们的作用是当内容到达边距时将其换行到新行,但是,当内容有间距时,内容不会被换行,例如:

\enablehyph{some other text here to wrap dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd}

无论是使用自定义宏还是使用\seqsplit内容都不会换行。

有没有办法可以包装示例场景中的内容? 无论是自定义宏、新命令还是应用于整个文档的方法。

答案1

我不确定这个想法是什么,因为\enablehyph它意味着打破任何地方

无论如何,这是一个尊重空格的版本。

\documentclass{article}

\ExplSyntaxOn

\NewDocumentCommand{\breakanywhere}{m}
 {
  \danmaia_breakanywhere:n { #1 }
 }

\seq_new:N \l__danmaia_breakanywhere_seq

\cs_new_protected:Nn \danmaia_breakanywhere:n
 {
  \seq_set_split:Nnn \l__danmaia_breakanywhere_seq { ~ } { #1 }
  \seq_map_function:NN \l__danmaia_breakanywhere_seq \__danmaia_breakanywhere_word:n
  \unskip\unskip
 }

\cs_new_protected:Nn \__danmaia_breakanywhere_word:n
 {
  \tl_map_inline:nn { #1 } { ##1 \skip_horizontal:n {0pt plus 0.1pt} }
  \c_space_tl
 }

\ExplSyntaxOff

\begin{document}

\breakanywhere{
  some other text here to wrap 
  ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd%
  ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd%
  dddddddddddddddddddd
  some other text here to wrap 
  ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd%
  ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd%
  dddddddddddddddddddd
  some other text here to wrap 
  ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd%
  ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd%
  dddddddddddddddddddd
  some other text here to wrap%
}

\end{document}

然而,输出结果却非常可疑。

在此处输入图片描述

答案2

这里,\enablehyphA是一个中间宏,它将输入分成空格分隔的块,并将每个块传递到原始宏(现在名为)\enablehypAA

\documentclass{article}

\begin{document}
\def\enablehyph#1{\iftrue\enablehyphA #1 \end\fi}
\def\enablehyphA#1 #2\fi{\fi\enablehyphAA#1\end
   \ifx\end#2\else\ \expandafter\enablehyphA#2\fi}
\def\enablehyphAA#1{\ifx\end#1%
   \else#1\penalty0\hskip0pt plus1pt\relax
   \expandafter\enablehyphAA\fi}

\enablehyph{This is a test dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd}
\end{document}

在此处输入图片描述

对于 OP 在评论中提到的更苛刻的输入(宏),这个令牌循环可能更适合处理以下问题:

\documentclass{article}
\usepackage{tokcycle}
\newcommand\enablehyph[1]{\tokcycle
  {\addcytoks{##1\penalty0\hskip0pt plus1pt\relax}}
  {\processtoks{##1}}
  {\addcytoks{##1}}
  {\addcytoks{##1}}
  {#1}\the\cytoks}
\begin{document}
\enablehyph{This is a test dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd}

\enablehyph{{\textasciitilde} ! @ \# \$ \% {\textasciicircum} \& * ( ) {\textless} 
{\textgreater} \{ \} [ ] \_- + $\beta$ $\beta$ $\alpha$ $\alpha$ $\surd$ $\surd$ 
$\sum$ $\Pi$ $\Pi$ $\neq$ $\neq$ $\pi$ $\pi$ {\textasciitilde} ! @ \# \$ \% 
{\textasciicircum} \& * ( ) \{ \} [ ] \_- + = \space unc mattis eget. 
Etiam vel tincidunt dolor. \space C:\textbackslash personal\textbackslash 
folder\textbackslash another folder\textbackslash space \textbackslash textcolor 
} 
\end{document}

在此处输入图片描述

相关内容