LaTeX forum ⇒ Text FormattingHighlighting a Chunk of Code within a lstlisting

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
gadgetto
Posts: 11
Joined: Wed Oct 24, 2012 8:28 am

Highlighting a Chunk of Code within a lstlisting

Postby gadgetto » Mon Mar 31, 2014 5:19 pm

Hi everyone,

I have got some lines of code inserted by lstlisting:
  1. \begin{lstlisting}
  2. public class MyRunnable implements Runnable{
  3. String name;
  4. public MyRunnable(String name){
  5. this.name = name;
  6. }
  7. \end{lstlisting}

Now I want to hightlight for example >>class MyRunnable<<. An easy way is to use colorbox:
  1. \begin{lstlisting}
  2. public §\colorbox{yellow}{class MyRunnable}§ implements Runnable{
  3. String name;
  4. public MyRunnable(String name){
  5. this.name = name;
  6. }
  7. \end{lstlisting}

It works fine, but you are losing the syntax highlighting inside a colorbox.

Is there a way to save the syntax highlighting???
I tested to insert a "\lstinline" in the "lstlisting", but I had no success :(

Has anyone an idea how to solve my problem???

best regards,
Gadgetto

esdd
Posts: 36
Joined: Sat Jan 25, 2014 9:13 pm

Postby esdd » Wed Apr 02, 2014 5:55 pm

You can use hf-tikz:

  1. \documentclass{scrartcl}
  2. \usepackage{listings}
  3. \lstset{escapechar=§}
  4.  
  5. \usepackage[norndcorners,customcolors]{hf-tikz}
  6. \hfsetbordercolor{yellow}
  7. \hfsetfillcolor{yellow}
  8.  
  9. \begin{document}
  10. \begin{lstlisting}
  11. public §\tikzmarkin{glw}§class MyRunnable§\tikzmarkend{glw}§ implements Runnable{
  12. String name;
  13. public MyRunnable(String name){
  14. this.name = name;
  15. }
  16. \end{lstlisting}
  17. \end{document}


Run twice to get the result:
listing.png
listing.png (7.79 KiB) Viewed 3938 times


Best regards
Elke


Return to “Text Formatting”

Who is online

Users browsing this forum: No registered users and 4 guests