2

I'm trying to "decorate" source code with listings, but I'm getting some warnings and bad boxes when I compile my document. However the document compiles and looks like I want.

I noticed the warning is due to some } and { symbols in the code.

Overfull \hbox (6.0pt too wide) in paragraph at lines 31--31 [][][][][][][] []

LaTeX Font Info: Try loading font information for OMS+cmss on input line 32.

LaTeX Font Info: No file OMScmss.fd. on input line 32.

LaTeX Font Warning: Font shape OMS/cmss/m/n' undefined (Font)
using
OMS/cmsy/m/n' instead (Font) for symbol `textbraceleft' on input line 32.

\documentclass[a4paper]{article}

\usepackage[spanish,activeacute]{babel}
\usepackage{amsmath}
\usepackage{listings}
\usepackage{color}
\usepackage{xcolor}
\usepackage{caption}

\lstset{
basicstyle=\footnotesize,
numbers=left,
numberstyle=\tiny,
frame=tb,
columns=fullflexible,
showstringspaces=false
}


\renewcommand{\familydefault}{\sfdefault}  %this is because default font looks blurry

\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{black}{\parbox{\textwidth}{#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}

\begin{document}

Some Code:
\begin{lstlisting}[caption=some code, label=source, language=java]
public void hello() {
    aaaa();
}
\end{lstlisting}

\end{document}
2

Use the Cork encoding:

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[spanish,activeacute]{babel}
\usepackage{amsmath}
\usepackage{listings}
\usepackage{color}
\usepackage{xcolor}
\usepackage{caption}

\lstset{
basicstyle=\footnotesize,
numbers=left,
numberstyle=\tiny,
frame=tb,
columns=fullflexible,
showstringspaces=false
}


\renewcommand{\familydefault}{\sfdefault}  %this is because default font looks blurry

\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{black}{\parbox{\dimexpr\textwidth-2\fboxsep\relax}{#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}

\begin{document}

Some Code:
\begin{lstlisting}[caption=some code, label=source, language=java]
public void hello() {
    aaaa();
}
\end{lstlisting}

\end{document}

I also fixed an overfull box (you didn't have into account the \fboxsep lengths introduced by the \colorbox).

enter image description here

Perhaps you could be interested in this answer from Axel Sommerfeldt (the author of caption) about \DeclareCaptionBox.

3
  • Great!. By the way, all this kind of stuff need to be done by hand?, there isnt some visual editor to design or something similar?. I ask you this because I started using latex few days ago, and every day I spend hours to customize the document to mi needs. – Wyvern666 Aug 14 '13 at 23:56
  • @Wyvern666 not that I know of. – Gonzalo Medina Aug 14 '13 at 23:57
  • BTW: The caption stuff (three lines of code) can be reduced to the single line \captionsetup[lstlisting]{box=colorbox,boxcolor=black,font={color=white},slc=off}, at least since caption v3.3. (colorbox takes account of \fboxsep.) – user2574 Aug 16 '13 at 5:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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