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.

Objective

I'm trying to achieve the following output.

enter image description here

Here's the code I used to produce it. I had to hack it

\begin{document}
\begin{description}

\item In \texttt{./build/org.eclipse.cdt.managedbuilder.core/plugin.xml} 
\\ there is an extension  
{\btHL \texttt{org.eclipse.cdt.managedbuilder.language} } 
{ \btHL \texttt{.settings.providers.GCCBuildCommandParser}} 
corresponding to the name \texttt {GCCBuildOutputParser.name}.
This is extending at the point 
{\btHL \texttt{org.eclipse.cdt.core.LanguageSettingsProvider}}

\end{description}
\end{document}

As you can see, I had to manually insert line breaks to typeset it correctly. I'd like to have LaTeX automatically break the lines also.

The preamble that defines \btHL was taken from Highlight text in code listing while also keeping syntax highlighting

Why?

As I've shown in the example above, java classes can be really long string with no spaces. And sometimes you need to mention them inline. On top of that I think highlighting them(adding background color) will enhance the readability. As it's java code I want to typeset it in monospace. Syntax highlighting is not a high priority though.

The Challenge

So far I've looked at the following answers for wrapping text

And the following links for Highlighting and background color.

It's very tedious to try every combination of techniques from both the sets but I tried many. It seems like the hard part is that both the applications use some kind of walk through technique, the first to calculate the line break points and the second to calculate the size of the box. If it wasn't inline then setting background color wouldn't require and fancy computation and this would be doable.

Summary

So in summary, how do I achieve the same output as above without having to manually insert line breaks? I don't want to worry about typesetting while typing the reports/journal entries.

share|improve this question

1 Answer 1

up vote 4 down vote accepted

Two types of macros are defined. The \plaintt breaks at line endings (without any additional hyphen character added). The other one \highlighttt allows breaks only at dots, and colors the text. No break can occur in between dots.

Update: I have made the thing a bit customizable. Let me comment on the difference between the two macros: \highlighttt has the disadvantage or advantage that breaks occur only at dots. But it has the big advantage that somewhat arbitrary TeX code in-between the dots can be used. See the first paragraph in the example, where I highlighted one sub-piece in blue.

The second macro \plaintt accepts basically only letters and punctutations, but anything can be escaped from it by just putting it within braces. This is illustrated in the second paragraph.

A hook is provided so that also \plaintt can do some highlighting, character per character. See the third paragraph where only \plaintt is used, but the hook has been set to use the yellow box highlighting method.

The highlighting method for \highlighttt is specified in the command \myhighlightmethod.

Only the use of the txtt font is hardcoded in the two macros, but of course this could be changed.

\documentclass{article}
%\usepackage[T1]{fontenc}
\usepackage[textwidth=12cm]{geometry}
%\usepackage{xcolor}
\usepackage{color}


% ``private macros''
\makeatletter
\def\@highlightttpeeknext{\futurelet\@nexttoken\@highlightttaux}
\def\@highlighttt #1.{%
    \def\@highlightttaux{\ifx\@nexttoken\egroup
       \myhighlightmethod {#1}\else
       \myhighlightmethod {#1.}\linebreak[2]%
       \expandafter\@highlighttt\fi}%
    \@highlightttpeeknext}

\def\@plaintt {\futurelet\@nexttoken\@plainttaux}
\def\@plainttaux {\ifx\@nexttoken\egroup\else
                  \ifx\@nexttoken\bgroup
                  \expandafter\expandafter\expandafter\@plaintta\else
                  \expandafter\expandafter\expandafter\@plainttb\fi\fi}
\def\@plaintta #1{{#1}\@plaintt}
\def\@plainttb #1{\ifcat\@nexttoken a\penalty\hyphenpenalty \plaintthook
  #1\else \plaintthook{#1}\linebreak[2]\fi\@plaintt}



% ``commands''

\newcommand{\highlighttt}[1]{{\fontfamily{txtt}\selectfont
   \@highlighttt #1.}}

\newcommand\plaintt{\bgroup\fontfamily{txtt}\selectfont
   \afterassignment\@plaintt\let\next= }

\makeatother

% ``customization''
\newcommand{\myhighlightmethod}[1]{\fboxsep0pt\colorbox{yellow}{\strut#1}}
\newcommand{\plaintthook}{}



\begin{document}\thispagestyle{empty}

\begin{description}

\item In \plaintt{./build/org.eclipse.cdt.managedbuilder.core/plugin.xml}
  there is an extension \highlighttt{org.eclipse.cdt.{\color{blue}managedbuilder}.language.settings.providers.GCCBuildCommandParser}
  corresponding to the name \plaintt{GCCBuildOutputParser.name}. This is
  extending at the point 
  \highlighttt{org.eclipse.cdt.core.LanguageSettingsProvider}

\item In \highlighttt{./build/org.eclipse.cdt.managedbuilder.core/plugin.xml}
  there is an extension \plaintt{org.eclipse.cdt.{\color{blue}managedbuilder}.language.settings.providers.GCCBuildCommandParser}
  corresponding to the name \highlighttt{GCCBuildOutputParser.name}. This is
  extending at the point 
  \plaintt{org.eclipse.cdt.core.LanguageSettingsProvider}

\renewcommand{\plaintthook}[1]{\myhighlightmethod{#1}}

\item In \plaintt{./build/org.eclipse.cdt.managedbuilder.core/plugin.xml}
  there is an extension \plaintt{org.eclipse.cdt.{\color{blue}managedbuilder}.language.settings.providers.GCCBuildCommandParser}
  corresponding to the name \plaintt{GCCBuildOutputParser.name}. This is
  extending at the point 
  \plaintt{org.eclipse.cdt.core.LanguageSettingsProvider}

\end{description}


\end{document}

highlighting wrapped code

share|improve this answer
    
Great! Thanks jfbu for the recipe. I don't know enough LaTeX to comment on the code, but I copy pasted this code and it works! Hope this helps others in future also. I will accept this answer for now, others are welcome to comment on the code and improve it, or provide other, better answers. –  Spundun Jan 23 '13 at 17:54
    
You are welcome. I updated my answer with a slightly extended code. –  jfbu Jan 23 '13 at 18:10
    
Thanks, in the output (by TeXSHop) I see faint one pixel lines in between the highlights, as if the string was split into multiple chunks even where not needed. I guess I could live with it for now. –  Spundun Jan 23 '13 at 18:31
    
@Spundun this is (I think) a problem with PDF rendering under Mac OS X. I have been confronted with it already. If you can, compile to dvi and look at the result with xdvi (you need XQuartz (or X11) installed). –  jfbu Jan 23 '13 at 18:33
    
@Spundun or simpler view the PDF in Acrobat! it does not use the Mac OS X pdf engine, and maybe you won't see the artefacts. –  jfbu Jan 23 '13 at 18:35

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.