2

My document is a mixture of regular text and code, where I format the code with \texttt{}. To make this a little easier, I added a new command:

\newcommand {\ttt} {\texttt}

so now I just write, \ttt{my code} whenever I'm referencing code. The problem is that these code sections often have very long "words" between the whitespaces, so I'd like LaTeX to insert linebreaks into my code arbitrarily, without worrying about whether it's at a whitespace or not. Is there some way to add this rule to my new command?

(Edit) Here is a minimal working example of a document where I'd like to have the line break added to the middle of the function name:

\documentclass{article}

\newcommand {\ttt} {\texttt}

\begin{document}
One of the functions in my software is \ttt{myVeryLongFunctionName.MoreFunctionName(parameter1,parameter2,parameter3,parameter4,parameter5)}. It's a very nice function.

\end{document}
4
  • Welcome to the site. Do the "words" hyphenate properly when you are not using your \ttt macro? If not, then the problem isn't with \ttt, but rather with the hyphenation of your particular set of really long words. Ideally, you should edit your question and provide a small self-contained example of a document that shows what you are describing. – Steven B. Segletes Apr 28 at 18:34
  • Hi, welcome to TeX.SE --- this question may be relevant: tex.stackexchange.com/q/219445/158639 – chsk Apr 28 at 20:09
  • @StevenB.Segletes Thanks for your comment. I just added a minimal working example. The words hyphenate properly according to the \texttt rules either with or without the macro, but the issue is that I'd like to have line breaks at any location in the text, not just at whitespace characters. – user2419194 Apr 29 at 20:42
  • @chsk Thank you for the link. That solution is a substantial improvement, and maybe even ideal for my needs (it allows line breaks at periods and open brackets). However, for the sake of completeness, I'm wondering if there's a way to allow line breaks between any two characters at all. – user2419194 Apr 29 at 20:45
1

While there are a number of answers at Linebreaks in long character strings, here is a new one that adds some unique features. In particular, macros and grouping can be embedded in the processed text without a problem. Additionally, glue is automatically employed to help with nice margins even in tight columns.

I use a tokcycle environment to process each token in the environment. If it is a printing character, I set it with a little wiggle-glue for margination and \allowbreak. Macros, groups, and spaces are just echoed directly to the output.

\documentclass{article}
\usepackage{tokcycle,xcolor,lmodern}
\xtokcycleenvironment\autobreak
{\addcytoks{##1\hspace{0pt plus 1pt minus.5pt}\allowbreak}}
{\processtoks{##1}}
{\addcytoks{##1}}
{\addcytoks{##1}}
{\ttfamily}
{}
\textwidth=2in
\begin{document}
Here is the technique: \autobreak
5A0FF349ABC5A0FF349ABC5A0FF349ABC5A0FF%
349ABC5A0FF349ABC5A0FF349ABC5A0FF349AB%
C5A0FF349ABC5A0FF349ABC5A0FF349ABC5A0F%
F3\endautobreak. Now I am almost done.

Let's try the OP's function: \autobreak 
myVeryLongFunctionName.MoreFunctionName%
(parameter1,parameter2,parameter3,parameter4,parameter5).%
\endautobreak{} It's a very nice function.

Now let's try it with embedded macros and grouping: 
\autobreak myVeryLongFunctionName.MoreFunctionName%
(\textcolor{|red|}{parameter1},\textbf{parameter2},%
\textit{parameter3},\textsf{parameter4},%
\textsc{parameter5})\endautobreak. Cool.

\end{document}

enter image description here

In the final paragraph, in order to prevent the color specification red from being contaminated with wiggle glue and \allowbreak, I use the tokcycle escape feature and delimit it with |red|, so that it passes through without processing.

4
  • @user2419194 You can go here, ctan.org/pkg/tokcycle. The files needed to run are tokcycle.sty and tokcycle.tex. The others are documentation and example files. – Steven B. Segletes May 1 at 0:54
  • This result looks great. However, I tried it and got the error that "File 'tokcycle.sty' not found". I'm using the Mac TeXShop distribution, so I don't know if it's standard in other distributions. On the other hand, the link you included has exactly what I need. Thanks! – user2419194 May 1 at 0:54
  • 1
    Success! Thank you. – user2419194 May 1 at 1:03
  • @user2419194 Great! And thank you for the check mark! – Steven B. Segletes May 1 at 1:32

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.