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 need to change dynamically the code in the listing by adding just one key word to the code. I tried to do it like this:

\begin{lstlisting}[escapechar=|]
  class MyThread extends Thread {
  private |\only<3>{\alert{volatile}}| boolean stop = false;

  public void run() {
    while (!stop) {
        doSomeWork();
    }
  }
  public void setStop() {
    this.stop = true;
  }
}
\end{lstlisting}

It works ok, but there are two spaces between "private" and "boolean" when the "volatile" is not visible. Is there any other way to make what I want?

share|improve this question
3  
Could you make this into a minimal working example (MWE)? However, placing a space between the two braces and removing the one after the closing escape | seems to do. –  egreg Feb 1 '13 at 11:52
    
I also thought of this. But the funny thing: when I do it like you said, then "boolean stop = false;" ends up on the next line. And when I do it like in example, it's on one line. –  damluar Feb 1 '13 at 14:36
    
I will do MWE now –  damluar Feb 1 '13 at 14:38
    
Ok, I got it working. I had two columns in a frame. Increasing width of the one, helped to avoid line break. You can put your comment to answers, thanks! –  damluar Feb 1 '13 at 15:32
add comment

closed as unclear what you're asking by Jubobs, Torbjørn T., Claudio Fiandrino, Svend Tveskæg, Jesse Mar 2 at 12:53

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

If you don't mind me changing your implementation slightly, here is my solution. Put your code in a separate file and then bring it in like the following example. By doing so, first you avoid any kind of unexpected behaviors like the one you are having now, and second, you can easily and simply add as many keywords as you want:

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}


\begin{document}
\lstset{
        language=Java,
        tabsize=3,
        %frame=lines,
        caption=A test file,
        label=code:test,
        frame=shadowbox, 
        rulesepcolor=\color{gray},
        xleftmargin=20pt,
        framexleftmargin=15pt,
        keywordstyle=\color{blue}\bf,
        commentstyle=\color{OliveGreen},
        stringstyle=\color{red},
        numbers=left,
        numberstyle=\tiny,
        numbersep=5pt,
        breaklines=true,
        showstringspaces=false,
        basicstyle=\footnotesize,
        %add your keywords here:
        emph={volatile},emphstyle={\color{magenta}}}
        %load the file here:
        \lstinputlisting{test.java}

\end{document}

The keywords are added in the last line of lstset (emph={volatile}). You can add more by separating them with ,

enter image description here

share|improve this answer
add comment

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