5

My TeX have a lot of inline code to compile, so sometimes the output of PDF seems like this:

enter image description here

The outlined part is coded in {\tt set_task_state(current_state)}, How to automatically break the line within the set_task_state(current_stat)?

2 Answers 2

4

Line breaking for monotype font is a hard issue. Below, five options (really less since some of them produce results which are not acceptable); I'd suggest you to use the listings package instead of just \ttfamily (by the way, \tt is deprecated and shouldn't ne used anymore) to write your listings. I loaded showframe to have a visual guide of the text area.

\documentclass{article}
\usepackage{listings}
\usepackage{underscore}
\usepackage{showframe}

\lstset{
  basicstyle=\ttfamily\small,
  columns=fullflexible,
  breaklines=true
}

\begin{document}

The method {\ttfamily set_current_state(current_state)} is synonimous to {\ttfamily set_task_state(current_state)}

The method \lstinline|set_current_state(current_state)| is synonimous to \lstinline|set_task_state(current_state)|

{\sloppy
The method \lstinline|set_current_state(current_state)| is synonimous to \lstinline|set_task_state(current_state)|\par}

{\sloppy
The method {\ttfamily set_current_state(current_state)} is synonimous to {\ttfamily set_task_state(current_state)}\par}  

{\raggedright
The method \lstinline|set_current_state(current_state)| is synonimous to \lstinline|set_task_state(current_state)|\par}

{\raggedright
The method {\ttfamily set_current_state(current_state)} is synonimous to {\ttfamily set_task_state(current_state)}\par}

{{\ttfamily\hyphenchar\the\font=`\-}%
The method {\ttfamily set_current_state(current_state)} is synonimous to {\ttfamily set_task_state(current_state)}\par}

\end{document}

The result

enter image description here

Some comments

Using linebreaks=true enables line-break points; however, in this case, the verbatim text still protrudes.

Using sloppy leaves horrible big gaps between words.

Using \raggedright gives a somehow better and acceptable result here.

1

I recommend to combine two methods: to allow breakpoints at _ characters and to allow larger spaces if needed. The first method is realized by setting _ as active (using \tth instead \tt) and the second method needs to set the \emergencystretch register to the appropriate value:

{\catcode`\_=13 \gdef_{\string_\-}}
\def\tth{\tt\catcode`\_=13 }
\emergencystretch=3em

\hsize=14cm % for example

The method {\tth set_current_state(current_state)} is synonimous to
{\tth set_task_state(current_state)}.

\bye

You are probably using plain TeX (congratulations) because LaTeX users hate the \tt. So, my example is for plain TeX.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .