1

My question is similar to Wrap long text content that have no spaces where there are two answers to wrap content such as

\seqsplit{ATGTCCACTGATAAAAGTACACGCTATAATTTTCAGATTGAGAAAGCCCCCTCGTTGGCGTACGCTGCAGGTCGAC}

or the custom macro mentioned in the answer above

\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}

These work and they do the job in that the content is wrapped onto a new line when reaching the margings, however, the content is not wrapped when there is content with spacing, for example:

\enablehyph{some other text here to wrap dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd}

Using either the custom macro or using \seqsplit the content is not wrapped onto a new line.

Is there a way I can wrap the content in the example scenario? Either a custom macro, a new command or away to apply across the whole document.

root92
  • 125
  • Can you explain why having “some other text here to wrap” inside \enablehyph would be necessary? – egreg Jul 06 '21 at 14:04
  • Not in that case, I just want to the content to be within in the margins regards the length of the text – root92 Jul 07 '21 at 14:54

2 Answers2

0

Here, \enablehyphA is an intermediate macro that separates the input into space-separated chunks and passes each chunck onto the original macro, now named \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}

enter image description here

For the more demanding inputs (macros) that the OP mentions in a comment, this token cycle is perhaps better prepared to handle the issues:

\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}

enter image description here

  • This works thank you, however, if I use special characters in it though such as this:

    \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}

    – root92 Jul 07 '21 at 14:50
  • I get errors because I think it's reading it in Math mode? – root92 Jul 07 '21 at 14:52
  • @DanMaia For one, your input has an underscore _ that is not in math mode. That will surely throw an error. – Steven B. Segletes Jul 07 '21 at 14:54
  • I used \_ and it still fails – root92 Jul 07 '21 at 15:21
  • @DanMaia Please see my edit. – Steven B. Segletes Jul 07 '21 at 16:40
  • Ah this is perfect but it doesnt work in latex 2018 – root92 Jul 08 '21 at 16:40
  • @DanMaia You can download tokcycle.tex and tokcycle.sty from https://ctan.org/pkg/tokcycle and place them in your localtexmf or, failing that, in your current working directory. – Steven B. Segletes Jul 08 '21 at 16:45
  • Perfect thank you, this works – root92 Jul 13 '21 at 08:33
0

I'm not sure what the idea is, because \enablehyph is meant to break anywhere.

For what it's worth, here's a version that respects spaces.

\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}

However, the output is very dubious.

enter image description here

egreg
  • 1,121,712