1

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}

enter image description here

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

Andrew Swann
  • 95,762
  • 2
    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. – David Carlisle 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? – Rasmus Bækgaard Nov 26 '15 at 12:55
  • We are looking for something like \code{OfThisLong\allowbreak PeiceOfText} but without the space... – Rasmus Bækgaard Nov 26 '15 at 12:57
  • Try this one, hope it helps: \newcommand{\code}[1]{\@tfor\@temp:=#1\do{{\fontfamily{pcr}\selectfont\@temp}\allowbreak}} – Sunilkumar KS 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 – Rasmus Bækgaard Nov 26 '15 at 13:07
  • Looks strange, this is my code:

    `\documentclass[a4paper]{article}

    \usepackage{showframe}

    \makeatletter \newcommand{\code}[1]{@tfor@temp:=#1\do{{\fontfamily{pcr}\selectfont@temp}\allowbreak}}

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

    \end{document}`

    – Sunilkumar KS Nov 26 '15 at 13:11
  • What does work is this suggestion: http://tex.stackexchange.com/a/26177/31636. But it removes the font... – Rasmus Bækgaard Nov 26 '15 at 13:11
  • If the problem persists, you might try with: \usepackage[T1]{fontenc} – Sunilkumar KS Nov 26 '15 at 13:13
  • It persists - even with T1: http://imgur.com/YW35Qen. Is it my compiler? – Rasmus Bækgaard Nov 26 '15 at 13:14
  • perhaps you want this automatic breaking for camel case: http://tex.stackexchange.com/questions/66593/automatic-camel-case-breaking/66603#66603 – David Carlisle Nov 26 '15 at 13:15
  • That is a very good suggestion, @DavidCarlisle - but it's not enough: http://i.imgur.com/ZJk6JpL.png - but better than nothing! – Rasmus Bækgaard Nov 26 '15 at 13:20
  • Strange to see Undefined control sequence error for \code in http://imgur.com/YW35Qen! – Sunilkumar KS Nov 26 '15 at 13:27
  • Your camels need more humps:-) If you add \sloppy before the paragraph the white space will stretch to allow breaking – David Carlisle Nov 26 '15 at 13:29
  • I'm not a big fan of \sloppy - it makes the spaces way too big. – Rasmus Bækgaard Nov 26 '15 at 13:34

2 Answers2

2

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{"}
\addto\extrasenglish{\languageshorthands{ngerman}}
\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}

enter image description here

  • 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?

    – Rasmus Bækgaard Nov 26 '15 at 13:53
  • Should OfThisLongPeiceOfText be hyphenated or not. And yes, beramono defines a new monospace font. –  Nov 26 '15 at 14:01
  • It should not be. – Rasmus Bækgaard 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 –  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. – Rasmus Bækgaard Nov 26 '15 at 14:11
  • You can use babel and the german shortcuts and then \codeOf""ThisLong""PeiceOfText} Then it can be hyphenated at the "" without a hyphen. –  Nov 26 '15 at 14:15
  • I tried those - I cannot get it to work. Do you have an MWE? – Rasmus Bækgaard Nov 26 '15 at 14:20
  • see edited answer –  Nov 26 '15 at 14:36
1

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: enter image description here


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}

enter image description here

But not always:

text