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 want to add an overbrace to some text in a Scala code listing (using lstlisting).

\begin{lstlisting}
object TypeClass extends FooParMain {
  def newApp(args: Array[String]) = new FooParApp {
    def run {
      val x = new Matrix(Seq(Seq(1, 2), Seq(3, 4)))
      val dSeq = Array.fill(worldSize)(x).toDistSeq

      for (res <- dSeq.sumD) {
        pprintln(res, " worldSize = " + worldSize, res == x * worldSize)
      }
    }
  }
}
\end{lstlisting}

Is there any sane way of doing this without breaking the syntax highlighting? (I tried [mathescape], but it doesn't look good)

share|improve this question
    
welcome to tex.sx. where the brace? over the whole thing or over some words in the listing? –  David Carlisle Apr 10 '13 at 11:09
    
In this case, overbrace on exactly "Array.fill(worldSize)(x)" and another brace over ".toDistSeq". Btw, thank you :) –  Felix Apr 10 '13 at 11:31
add comment

2 Answers

This uses a bit of "by eye" measuring hard to measure it automatically without knowing exactly what listings will do with the text.

enter image description here

(Please for future questions post complete documents showing packages used as below)

\documentclass{article}
\usepackage{listings}

\begin{document}

\small
\begin{lstlisting}[mathescape]
object TypeClass extends FooParMain {
  def newApp(args: Array[String]) = new FooParApp {
    def run {
      val x = new Matrix(Seq(Seq(1, 2), Seq(3, 4)))
      val dSeq = $\rlap{\(\overbrace{\strut\hspace{4.5cm}}\)}$Array.fill(worldSize)(x).toDistSeq

      for (res <- dSeq.sumD) {
        pprintln(res, " worldSize = " + worldSize, res == x * worldSize)
      }
    }
  }
}
\end{lstlisting}

\end{document}
share|improve this answer
    
This solution messes up the layout (background and frame splits up at the line) –  Felix Apr 11 '13 at 10:49
    
you could make it $\smash{\rlap{...}} which would prevent the brace opening up the line height but then you would have to arrange in your general listings settings that the line height is big enough or the brace will over-print the line above. Or you could put the backgrpund ad frame on externally using mdframed which wouldn't care about the internal structure. If a frame is a requirement perhaps you should edit the question example to include one. –  David Carlisle Apr 11 '13 at 11:05
add comment

The following is an adaptation of the approach suggested in Highlight text in code listing while also keeping syntax highlighting. The basic idea is to use listings moredelim option to mark the part to overbrace.

With the moredelim=** syntax you can define delimiters which styles apply on top of all other formattings. In the following I use backticks as delimiters, but you may choose any character:

\lstdefinestyle{...}{
    moredelim=**[is][\btHL]{`}{`},
}

The only issue is the formatting commands to be applied (\btHL) do not get the text to highlight as a macro parameter. Instead, they have to work like the LaTeX font commands (e.g., \bfseries or \color) that take affect until the end of the current group. Hence, we have to catch the content in an lrbox.

Complete MWE (using Java instead of Scala to demonstrate the combination with syntax highlighting):

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{listings,xcolor,beramono,amsmath}


\makeatletter
\newenvironment{btHighlight}[1][]
{\begingroup\def\bt@Highlight@par{#1}\begin{lrbox}{\@tempboxa}}
{\end{lrbox}\bt@HL@box[\bt@Highlight@par]{\@tempboxa}\endgroup}

\newcommand\btHL[1][]{%
\begin{btHighlight}[#1]\bgroup\aftergroup\bt@HL@endenv%
}
\def\bt@HL@endenv{%
\end{btHighlight}%   
\egroup
}
\newcommand{\bt@HL@box}[2][]{%
  $\overset{\text{#1}}{\overbrace{\strut\usebox{#2}}}$%
}
\makeatother

\lstdefinestyle{Java}{
    language={Java},basicstyle=\ttfamily, 
    moredelim=**[is][{\btHL[class name]}]{`}{`},
    moredelim=**[is][{\btHL[important]}]{@}{@},
    escapechar={§},
}

\begin{document}

  A listing with {\btHL[overbrace text] highlighting of all \textbf{important} elements} looks as follows:


\begin{lstlisting}[style=Java]
  public class `HelloWorld` {
    public @static void@ main(String[] args) {
      System.out.println("Hello, World");
    }
  }
\end{lstlisting}
\end{document}

enter image description here

share|improve this answer
    
Sorry for adding this so late, but I would like to be able to put text at the overbrace. Is this possible with this approach? –  Felix Apr 10 '13 at 20:22
    
@Felix: Please see updated code and screen shot. The issue is how to pass the overbrace text. In the updated solution, it can be given as optional parameter to the \btHL[<overbrace text>] command. Basically this means that you have to define an own moredelim for every overbrace text. Does this work for you? –  Daniel Apr 10 '13 at 21:20
add comment

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.