TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

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

  public void run() {
    while (!stop) {
  public void setStop() {
    this.stop = true;

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

closed as unclear what you're asking by Jubobs, Torbjørn T., Claudio Fiandrino, Svend Tveskæg, Jesse Mar 2 '14 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.

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

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:


        caption=A test file,
        %add your keywords here:
        %load the file here:


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

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