Text Formattingframebox using colored background, verbatim text & shadow

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
phitex
Posts: 7
Joined: Wed Apr 18, 2012 12:17 pm

framebox using colored background, verbatim text & shadow

Post by phitex »

Hi all

I am looking for a simple way to define a newcommand that formats my source code the following way.

like this (ceated with ms-word):
soll.png
soll.png (22.23 KiB) Viewed 9582 times
  • light yellow background
  • verbatim text
  • possibility to write some words in bold or italic
  • black border around the box, which is right and left double as thick that looks like a light shadow.


I have found

Code: Select all

\lstset{%
  backgroundcolor=\color{codeBackground},%
  frame=single,%
  mathescape=true,%
}
(where codeBackground is defined before.)

This produces the following output:
ist.png
ist.png (15.38 KiB) Viewed 9582 times

which does nearly what I need, but I want to have no white lines between the text lines and I dont want to have any other formatting than bold an italic.

Any Idea?

Thanks in advance.

φ
Last edited by Stefan Kottwitz on Thu Apr 19, 2012 2:29 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

framebox using colored background, verbatim text & shadow

Post by cgnieder »

Would you mind posting a complete Infominimal working example? With the code you posted so far it is not possible to reproduce the example you give and it's quite tedious to figure out everything by oneself...

Besides: what do you me by “I want to have no white lines between the text lines”? I can see no white lines?!

Maybe you can use this Infominimal working example as a starting point for your's so we can use it to help you?

Code: Select all

\documentclass{article}
\usepackage{listings,xcolor}
\colorlet{codeBackground}{yellow!20}
\lstset{%
  backgroundcolor=\color{codeBackground},%
  frame=single,%
  mathescape=true,%
}
\begin{document}

\begin{lstlisting}
public class Test {
  public static void main(String[] argumente) {
    System.out.printIn("Hallo Welt!");
  }
}
\end{lstlisting}

\end{document}
Regards
site moderator & package author
mas
Posts: 226
Joined: Thu Dec 04, 2008 4:39 am

framebox using colored background, verbatim text & shadow

Post by mas »

cgnieder wrote: Besides: what do you me by “I want to have no white lines between the text lines”? I can see no white lines?!
The white lines can be seen clearly if you change the color. I used this

Code: Select all

\colorlet{codeBackground}{red!80!black}
The gap between lines is not filled with the color.

Regards.

OS: Debian/GNU Linux; LaTeX System : TeXLive; Editor : Vim
phitex
Posts: 7
Joined: Wed Apr 18, 2012 12:17 pm

framebox using colored background, verbatim text & shadow

Post by phitex »

Hello everybody

Thanks for your recent answers.
The white lines are not visible in the dvi. But I can see them after processing to pdf.

But this is not the main problem.

The main issue is to have plain verbatim text but using bold and italic where "I" want to have it, and not where "lstlisting" proposes.

A second problem is the shadow of the box which can not be produced using lstlisting?? I have triede a bit using SaveVerbatim and BUseVerbatim, but without any success.


Here the complete minimal listing:

Code: Select all

\documentclass[twoside,12pt,a4paper]{article}

\usepackage{color,fancyvrb,fancybox}
\usepackage{listings}

\definecolor{codeBackground}{rgb}{0.9, 0.9, 0.8}

%%\lstset{language=Java}%

\lstset{%
  backgroundcolor=\color{codeBackground},%
  frame=single,%
  mathescape=true,%
}

\begin{document}
\begin{lstlisting}
public class Test {
  public static void main(String[] argumente) {
    System.out.println("Hallo Welt!");
  }
}
\end{lstlisting}
\end{document}
Last edited by Stefan Kottwitz on Fri Apr 20, 2012 9:16 am, edited 1 time in total.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

framebox using colored background, verbatim text & shadow

Post by cgnieder »

I would probably combine listings with mdframed. Use the former to format the code and the latter to format background and frame. The mdframed documentation shows many examples for the formatting of frame/background/shadow...

listings provides the command \lstnewenvironment that we can use to create a new custom listings environment. For the highlighting wherever and whenever you want you can use listings' moredelim option.

Code: Select all

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

\definecolor{codeBackground}{rgb}{0.9, 0.9, 0.8}
\lstnewenvironment{mylisting}{%
  \lstset{
    moredelim=**[is][\bfseries]{|}{|},% bold everything in between ||
    moredelim=**[is][\itshape]{*}{*}  % italic everything in between **
  }%
  \mdframed[backgroundcolor=codeBackground,shadow=true,shadowsize=2pt,shadowcolor=black!30]%
}{%
  \endmdframed\ignorespaces
}

\begin{document}

\begin{mylisting}
public |class| Test {
  public *static |void|* main(String[] argumente) {
    System.out.println("Hallo Welt!");
  }
}
\end{mylisting}

\end{document}
mdframed_listings.png
mdframed_listings.png (15.3 KiB) Viewed 9552 times
Regards
site moderator & package author
phitex
Posts: 7
Joined: Wed Apr 18, 2012 12:17 pm

Re: framebox using colored background, verbatim text & shado

Post by phitex »

Exactly what I was looking for.

Many many thanks!

φ
phitex
Posts: 7
Joined: Wed Apr 18, 2012 12:17 pm

Re: framebox using colored background, verbatim text & shado

Post by phitex »

Two minor problems:

* The Code should be texttt (typewriter)
* The module "mdframed" can not be loaded. So, a Frame is not visible.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

framebox using colored background, verbatim text & shadow

Post by cgnieder »

If mdframed cannot be loaded than you don't seem to have it installed.

In the documentation to listings (which is very comprehensive, imho) you'll find the option basicstyle. It can be used for the typewriter font. Add something like basicstyle=\ttfamily:

Code: Select all

\lstset{
    moredelim=**[is][\bfseries]{|}{|},% bold everything in between ||
    moredelim=**[is][\itshape]{*}{*}, % italic everything in between **
    basicstyle=\ttfamily
  }%
Regards
site moderator & package author
phitex
Posts: 7
Joined: Wed Apr 18, 2012 12:17 pm

Re: framebox using colored background, verbatim text & shado

Post by phitex »

Hello cgnieder

I will try this out after my holidays. But the solution I already have from you is realy great! :-)

thakgs again
Post Reply