3

I want to have long texts without spaces written in monospace font (using \texttt). As line feeds don't appear automatically, I found this answer: https://tex.stackexchange.com/a/315376, where a macro is suggested.

As I want the text to break at several different symbols, I tried to change the macro to accept a parameter with the sign to break at. Unfortunately, this doesn't work properly, as you can see in the screenshot below.

Screenshot of rendered text

Here is my version of the macro:

\documentclass{article}

\newcommand*\ttcomma[1]{\texttt{\expandafter\dottvar{\detokenize{#1}}{,}\relax}}
\newcommand*\dottvar[2]{%
\ifx\relax#1\else%
  \expandafter%
    \ifx\string#2#1%
      \string#2\allowbreak%
    \else#1
    \fi
\expandafter\dottvar\fi
}

\begin{document}
Some normal Text \ttcomma{someLongTextWithoutSpacesButWithCommas,WhereIWantToGetALineFeedAndBreakToTheNextLine}

\end{document}

Where did I go wrong? Bonus question: How can I change the macro to do a line break at several different signs (e. g. ,, :, -)

5

Here's an expl3 implementation, where you can set the allowed break points at runtime.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\ttlong}{O{\c_silvernak_ttlong_breaks_str}m}
 {
  \group_begin:
  \ttfamily
  \silvernak_ttlong:nn { #1 } { #2 }
  \group_end:
 }

% the default break points
\str_const:Nn \c_silvernak_ttlong_breaks_str { , : - }

\cs_new_protected:Nn \silvernak_ttlong:nn
 {
  \str_set:Nx \l__silvernak_breaks_str { #1 }
  \str_map_function:nN { #2 } \__silvernak_ttlong_char:n
 }

\cs_new_protected:Nn \__silvernak_ttlong_char:n
 {
  #1
  \str_if_in:NnT \l__silvernak_breaks_str { #1 } { \skip_horizontal:n { 0pt } }
 }

\ExplSyntaxOff

\begin{document}

\parbox{0pt}{
  Some normal Text \ttlong{someLongTextWithoutSpacesButWithCommas,WhereIWantToGetALine-FeedAndBreakTo:TheNextLine}
}

\bigskip

\parbox{0pt}{
  Some normal Text \ttlong[,]{someLongTextWithoutSpacesButWithCommas,WhereIWantToGetALine-FeedAndBreakTo:TheNextLine}
}

\bigskip

\parbox{0pt}{
  Some normal Text \ttlong[,:-a]{someLongTextWithoutSpacesButWithCommas,WhereIWantToGetALine-FeedAndBreakTo:TheNextLine}
}

\end{document}

The \parbox here is only used to force as many line breaks as possible.

enter image description here

The idea is to map the given string character by character; if the current character is in the list of allowed line break points, add a zero skip.

4

You can use the following, just add every character you want to be breakable to the definition of \breakable (inside the \detokenize, this is done so that the category codes are correct). The entire text is you input is \detokenized, and spaces are ignored inside the text.

EDIT: I somehow forgot to put a \ttfamily into the macro...

\documentclass[]{article}

\makeatletter
\edef\breakables{\detokenize{,:-.}}
\def\end@ttbreakable{\end@ttbreakable}
\newif\if@ttbreakable@break
\def\my@fi@gt\fi#1#2{\fi}
\newcommand*\ttbreakable[1]
  {%
    \begingroup
    \ttfamily
    \expandafter\@ttbreakable\detokenize{#1}\end@ttbreakable
    \endgroup
  }
\newcommand*\@ttbreakable[1]
  {%
    \ifx\end@ttbreakable#1%
      \my@fi@gt
    \fi
    \@firstofone
    {%
      #1%
      \def\@ttbreakable@test##1#1##2\end@ttbreakable
        {%
          \if\relax\detokenize{##2}\relax
          \else
            \allowbreak
          \fi
        }%
      \expandafter\@ttbreakable@test\breakables#1\end@ttbreakable
      \@ttbreakable
    }%
  }
\makeatother

\begin{document}
\hsize=5cm
\ttbreakable
  {%
    This is some text, with no spaces, but commas.
    It has: colons- hyphens and .periods, too.
  }
\end{document}
1

With some side effects you could do this with the url package. The text is treated as verbatim though which may not be what you want. But at least it provides an existing interface to set up the breaks.

MWE

\documentclass{article}

\usepackage{url}

\DeclareUrlCommand\ttbreakable{%
  \def\UrlBreaks{\do\,\do\:\do\-}%
  \def\UrlBigbreaks{}%
  \def\UrlNoBreaks{}%
  \def\UrlOrds{}%
  \def\UrlSpecials{}%
  \def\UrlFont{\ttfamily}}

\begin{document}

% Need a sloppy par otherwise the first line is too underfull and won't break
% at the first comma
\begin{sloppypar}
  Some normal Text
  \ttbreakable{someLongTextWithoutSpacesButWithCommas,WhereIWantToGetALine-FeedAndBreakTo:TheNextLine}
\end{sloppypar}

\parbox{0pt}{%
  Some normal Text
  \ttbreakable{someLongTextWithoutSpacesButWithCommas,WhereIWantToGetALine-FeedAndBreakTo:TheNextLine}}

\end{document}

MWE output

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.