2

Currently I have no idea why the shaded box adds this spacing at the bottom of my listing:

enter image description here

I'm not sure if the shaded box adds this spacing after the caption or if the listing just adds this extra space after the caption. That's my latex code for this listing:

\begin{shaded}
  \begin{lstlisting}[caption=Definition of a relation object., label=list:relation-object, language=JavaScript]
var UserProjectRelation = relation
  .from(User)
  .to(Project)
  .via("member");
  \end{lstlisting}
\end{shaded}

That's my setup for the listings:

\lstdefinelanguage{JavaScript}{
  keywords={typeof, new, true, false, catch, function, return, null, catch, switch, var, let, const, if, in, while, do, else, case, break, from},
  ndkeywords={class, export, boolean, throw, implements, import, this, string, number, boolean},
  sensitive=false,
  comment=[l]{//},
  morecomment=[s]{/*}{*/},
  morestring=[b]',
  morestring=[b]",
  inputencoding=utf8
}

\lstset{
   language=JavaScript,
   numbers=left,
   captionpos=b
}
\definecolor{shadecolor}{named}{sbase03}

\DeclareCaptionFormat{captionFormat}{%
  \color{white}{\ttfamily#1#2#3}
} 
\captionsetup[lstlisting]{format=captionFormat}

EDIT:

Here are my used packages and the doctype:

\documentclass[%
fontsize=\myfontsize,%% size of the main text
paper=\mypapersize,  %% paper format
parskip=\myparskip,  %% vertical space between paragraphs (instead of indenting first par-line)
DIV=calc,            %% calculates a good DIV value for type area; 66 characters/line is great
headinclude=true,    %% is header part of margin space or part of page content?
footinclude=false,   %% is footer part of margin space or part of page content?
open=right,          %% "right" or "left": start new chapter on right or left page
appendixprefix=true, %% adds appendix prefix; only for book-classes with \backmatter
bibliography=totoc,  %% adds the bibliography to table of contents (without number)
draft=\mydraft,      %% if true: included graphics are omitted and black boxes
                     %%          mark overfull boxes in margin space
BCOR=\myBCOR,        %% binding correction (depends on how you bind
                     %% the resulting printout.
\mylaterality        %% oneside: document is not printed on left and right sides, only right side
                     %% twoside: document is printed on left and right sides
]{scrbook}  %% article class of KOMA: "scrartcl", "scrreprt", or "scrbook".
            %% CAUTION: If documentclass will be changed, *many* other things
            %%          change as well like heading structure, ...

\usepackage[utf8]{inputenc} %% UTF8 as input characters

\usepackage[backend=biber, %% using "biber" to compile references (instead of "biblatex")
style=\mybiblatexstyle, %% see biblatex documentation
%style=alphabetic, %% see biblatex documentation
%dashed=\mybiblatexdashed, %% do *not* replace recurring reference authors with a dash
backref=\mybiblatexbackref, %% create backlings from references to citations
natbib=true, %% offering natbib-compatible commands
hyperref=true, %% using hyperref-package references
]{biblatex}  %% remove, if using BibTeX instead of biblatex

\addbibresource{\mybiblatexfile} %% remove, if using BibTeX instead of biblatex
\usepackage[pdftex]{graphicx}
\usepackage{pifont}
\usepackage{ifthen}
\usepackage{xspace}
\usepackage[usenames,dvipsnames,x11names,rgb,html]{xcolor}
\usepackage[normalem]{ulem}
\usepackage{framed}
\usepackage{eso-pic}
\usepackage{enumitem}
\usepackage{units}

\usepackage{listings}
\usepackage{float}
\usepackage{solarized-dark}
\usepackage{framed}
\usepackage{subcaption}
\usepackage{etoolbox}


\begin{document}

...

\end{document}
  • Welcome to TeX.SE!. Please include the document class and the required packages to your code to make it compilable. – Cragfelt Nov 23 '17 at 18:40
  • I am sorry I did not make myself clear. Please show your code as a full minimal working example (MWE). – Cragfelt Nov 23 '17 at 19:32
  • 1
    Try adding the line \mbox{}\vspace{-\dimexpr\topsep+\baselineskip\relax} after #1#2#3 in the definition of caption format (\DeclareCaptionFormat). This will short the extra space at the bottom of the box. – Cragfelt Nov 23 '17 at 19:55
2

I figured it out myself with the help of @Cragfelt! I put a minimal working example together and checked my custom solarized style.

There was this line in the style definition:

belowcaptionskip=1\baselineskip,

Which I changed to 0 and now it works, with the only exception that I added half of \baselineskip\relax to center the caption in the middle of the bottom space:

Final result

\documentclass[%
  fontsize=12pt,
  paper=A4,
  parskip=half,
  DIV=calc,
  headinclude=true,
  footinclude=false,
  open=right,
  appendixprefix=true,
  bibliography=totoc,
  draft=false,
  BCOR=0mm,
  twoside
]{scrbook}

\usepackage[utf8]{inputenc}

\usepackage[pdftex]{graphicx}
\usepackage{pifont}
\usepackage{ifthen}
\usepackage{xspace}
\usepackage[usenames,dvipsnames,x11names,rgb,html]{xcolor}
\usepackage[normalem]{ulem}
\usepackage{framed}
\usepackage{eso-pic}
\usepackage{enumitem}
\usepackage{units}

\usepackage{listings}
\usepackage{float}
\usepackage{solarized-dark}
\usepackage{framed}
\usepackage{subcaption}
\usepackage{etoolbox}

\lstdefinelanguage{JavaScript}{
  keywords={typeof, new, true, false, catch, function, return, null, catch, switch, var, let, const, if, in, while, do, else, case, break, from},
  ndkeywords={class, export, boolean, throw, implements, import, this, string, number, boolean},
  sensitive=false,
  comment=[l]{//},
  morecomment=[s]{/*}{*/},
  morestring=[b]',
  morestring=[b]",
  inputencoding=utf8
}

\lstset{
  language=JavaScript,
  numbers=left,
  captionpos=b
}
\definecolor{sbase03}{HTML}{002B36}
\definecolor{shadecolor}{named}{sbase03}

\DeclareCaptionFormat{captionFormat}{%
  \color{white}{\ttfamily#1#2#3%
  \mbox{}\vspace{-\dimexpr\topsep+0.5\baselineskip\relax}}
} 
\captionsetup[lstlisting]{format=captionFormat, font={color=white}, labelfont=bf}

\begin{document}

\begin{shaded}
  \begin{lstlisting}[caption=Definition of a relation object., label=list:relation-object, language=JavaScript]
var UserProjectRelation = relation
  .from(User)
  .to(Project)
  .via("member");
  \end{lstlisting}
\end{shaded}

\end{document}
  • Nice solution. I'm glad you found it and that I helped you somehow. – Cragfelt Nov 26 '17 at 0:26
1

Maybe this workaround can work

enter image description here

I could not compile your work with the code provided to display the exact listing box as shown and I had to emulate the colors like in your image. Neither the rules appear, due to some feature I couid not find. But for joy, the little addition you need to make, is not related to those features but to the extra blank space after caption. As my comment says, you need to add \mbox{}\vspace{-\dimexpr\topsep+\baselineskip\relax} after #1#2#3 in the definition of caption format (\DeclareCaptionFormat). This will short the extra space at the bottom of the box.

\DeclareCaptionFormat{captionFormat}{%
\color{white}{\ttfamily#1#2#3%
\mbox{}\vspace{-\dimexpr\topsep+\baselineskip\relax}}
} 
\captionsetup[lstlisting]{format=captionFormat}
  • Thank you a lot for your effort! I'm sorry that I didn't provide a working example. If I try this the caption gets centered in the space at the bottom of the listing after the code, but isn't any smaller. It's pretty late today, so I'm going to create a simple working example tomorrow, as I'm using a template from university for my bachelor thesis (Github Page of the template). – Jan Peter Nov 23 '17 at 21:27
  • Ok got it. Well, I'll be aware of this until tomorrow then, so I could edit my answer for that could meet your spectations. – Cragfelt Nov 23 '17 at 21:37

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.