Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am currently using the longtable environment and I have set the width for each row. In one particular row I need to include some Prolog code which happen to have underscore characters. For instance:

 predicate_name_test(argument1, argument2, argument3)

The issue is that latex does not seem to wrap the text with underscores (at least this is my educated guess, correct me if I am wrong) causing the Prolog code to exceed the width of the row. Can you help me, please?

Here the syntax I am using to insert a longtable:

 \begin{document}
 \begin{landscape}
 \begin{center}
 \scriptsize
 \begin{longtable}{|p{2cm}|p{4cm}|p{8cm}|}\hline\hline
 \multicolumn{1}{|p{2cm}|}{\textbf{Text1}} &
 \multicolumn{1}{p{4cm}|}{\textbf{Text2}} &
 \multicolumn{1}{p{8cm}|}{\textbf{Text3}}\\ \hline 
 \endfirsthead

\multicolumn{3}{p{3cm}}
{{\bfseries \tablename\ \thetable{} -- continued from previous page}}\\ 
 \hline\multicolumn{1}{|p{2cm}|}{\textbf{Text1}} &
 \multicolumn{1}{p{4cm}|}{\textbf{Text2}} &
 \multicolumn{1}{p{8cm}|}{\textbf{Text3}}\\\hline 
 \endhead

\hline \multicolumn{3}{|r|}{{Continued on next page}} \\ \hline
\caption[Caption]{Caption}\label{label-caption} 
\endfoot
\caption[Caption]{Caption}\label{label-caption} 
\endlastfoot

\end{longtable}
\end{center}
\end{landscape}
\end{document}
share|improve this question
2  
Please, instead of submitting only a code snippet, include an entire minimal working example (MWE) that illustrates your problem. This includes all the packages that you load (even though you may only use longtable and pdflscape in this case), as well as you \documentclass. This just helps interested community members to hit the ground running with solving your problem. That said, you can use the url package to help you here... –  Werner Feb 9 '12 at 2:21

2 Answers 2

One solution is to pass your code through some pre-processing filter to address printability issues. I run into these problems with file names and function names in longtables that present various metrics about those entities. I use this perl regular expression, $line =~ s/_/\\_\\hspace{0pt}/g; to make underscores breakable. In Python you could use line = re.sub('_', '_\hspace{0}', line) to achieve the same.

Another solution is to play with catcodes. Someone else is sure to come along with a clever catcode-based solution.

Yet another solution, and the one I recommend, is to adopt an 80 column mindset before you typeset your code. There is a very good reason for that mindset. Lines much longer than that are hard to read. When your eyes scan back from the end of one line to the start of the next, your eyes start losing track of where you were when lines start getting long. Study after study have shown that code is more readable when the lines are kept to some reasonable limit. Just because your screen can display very long lines does not mean that you should take advantage of that.

This loss of ability to keep track is a big part of why the default margins for LaTeX are so wide. Human factors and readability have played a big role in TeX and LaTeX from day one. Apply the same concepts to your code. You will find that for the most part it is much more readable when you keep line lengths reasonably small (80 characters or so is "reasonably small").

share|improve this answer
1  
I agree on the 80-column mindset, but sometimes in a paragraph of surrounding comments, you might need to give a name like predicate_name_test, and in that case you'd want it to break cleanly rather than having an underfull or overfull hbox. –  Todd Lehman Feb 9 '12 at 3:24
    
@ToddLehman: Absolutely. I have this problem in presenting metrics about the code, so a shortish column for the name of the entity plus other columns that contain metrics. (Our group long ago gave up on printing the code in LaTeX documents. If you want to RTFC, there are far better tools than a document.) –  David Hammen Feb 9 '12 at 4:19

One solution is to use \path from the url (or hyperref) package, which typesets the argument in typewriter font and splits it:

\documentclass{article}
\usepackage{url}
\pagestyle{empty}
\begin{document}
\hsize1cm
\noindent\path{predicate_name_test(argument1, argument2, argument3)}
\end{document}

enter image description here

share|improve this answer
1  
Note that if you use url, you can set the font used \UrlFont. So, it doesn't necessarily have to \ttfamily. –  Werner Feb 9 '12 at 3:14
1  
@Werner Surely. Actually \path is a great way to typeset custom quasi-verbatim text---much better than \verb, imho. –  Boris Feb 9 '12 at 3:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.