Custom command exceeds my textwidth

I have a problem I do not know how to handle:

I need to include some small words of code - these can sometimes be longer names of something. To display this is a name of a code piece, I've made a small command, \code{...} which wraps the ... in a special font.

But when the ... gets too long, the text exceeds my margins of the page.

MWE:

\documentclass[a4paper]{article}

\usepackage{showframe} % Just to show the frames

%A font for code
\newcommand{\code}[1]{{\fontfamily{pcr}\selectfont #1}}

\begin{document}
This is a longer line -- notice what happens now at the end: \code{OfThisLongPeiceOfText}
\end{document}


Any suggestions to how I can fix this? Forcing a \\ when I see it gives me problems or split it up with \code{firstPart}\code{SecondPart} ?

• you need to say what you want to happen, choices are break before the code, leaving the line short, break before the code stretching the line out, or allowing the code to break with, or without hyphenation. Nov 26 '15 at 12:52
• Lets say I want it to break without hyphenation. Do you have a suggestion of how it could be done? Nov 26 '15 at 12:55
• We are looking for something like \code{OfThisLong\allowbreak PeiceOfText} but without the space... Nov 26 '15 at 12:57
• Try this one, hope it helps: \newcommand{\code}[1]{\@tfor\@temp:=#1\do{{\fontfamily{pcr}\selectfont\@temp}\allowbreak}} Nov 26 '15 at 13:03
• Does not compile, @SunilkumarKS.  LaTeX Error: Command \ unavailable in encoding OT1. [...ow at the end: \code{OfThisLongPeiceOfText}]. It looks like it does not recognize the \allowbreak Nov 26 '15 at 13:07

in such cases use:

\begin{sloppypar}
This is a longer line -- notice what happens now at the end: \code{OfThisLongPeiceOfText}
\end{sloppypar}


and if you do not like \sloppypar use:

\usepackage[scaled=0.85]{beramono}
\usepackage{showframe} % Just to show the frames
\newcommand{\code}[1]{{\ttfamily#1}}


Or use the german shortcuts:

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{libertine}
\usepackage[scaled=0.85]{beramono}
\usepackage[ngerman,english]{babel}
\useshorthands{"}
\usepackage{showframe} % Just to show the frames
\newcommand\code[1]{{\ttfamily#1}}

\begin{document}
This is a longer line -- notice what happens now at the end: \code{OfThis""Long""Peice""OfText}
\end{document}


• It could seem like sloppybar does the same as seqsplit. Can you implement this as a \command without the word is moved to a new line? beramono overrides the \ttfamily? Nov 26 '15 at 13:53
• Should OfThisLongPeiceOfText be hyphenated or not. And yes, beramono defines a new monospace font.
– user2478
Nov 26 '15 at 14:01
• It should not be. Nov 26 '15 at 14:02
• Then use \newcommand\code[1]{{\mbox{\ttfamily#1}}} and sloppypar or set the paragraph with \RaggedRight (package ragged2e) or insert a \newline before \code
– user2478
Nov 26 '15 at 14:05
• I really want the \code word to be split up. sloppypar makes the whole line have huge spaces in it, which does not look good. \RaggedRight does not help, since it moves everything down, and \newline will do the same. Nov 26 '15 at 14:11

One way (not the best) is splitting it with the seqsplit package:

\documentclass[a4paper]{article}
\usepackage{showframe}
\usepackage{seqsplit}

\newcommand{\code}[1]{{\fontfamily{pcr}\selectfont \seqsplit{#1}}}

\begin{document}
This is a longer line -- notice what happens now at the end: \code{OfThsLongPeiceOfText}
\end{document}


Will give this:

But there exists solutions with CamelCase splitting (shamelessly taken from https://tex.stackexchange.com/a/66603/31636 as mentioned by @DavidCarlisle), which works most of the time:

\documentclass[a4paper]{article}
\usepackage{showframe}

\showhyphens{createUnspecifiedNodeErrorMarker}

\makeatletter
\def\code{\leavevmode\begingroup
\let\ifcase\iftrue
\def\or##1{%
\catcode##1\active\uccode\~##1\uppercase{%
\def~{\egroup\penalty2\hbox\bgroup\string##1}}}%
\@Alph{}%
\@code}

\def\@code#1{\textbf{\hbox\bgroup#1\egroup}\endgroup}
\makeatother

\begin{document}
This is a longer line -- notice what happens now at the end: \code{OfThisLongPeiceOfText}
\code{OfThisLongPeiceOfText}
\code{OfThisLongPeiceOfText}
\code{OfThisLongPeiceOfText}
\code{OfThisLongPeiceOfText}
\end{document}
`

But not always: