4

I want to decrease the line spaces of the java codes in the following latex. I already used \setstretch{0.4} but it seems that this code does not work for listing contents. how should I do this??

\begin{filecontents*}{FristProgram.java}
import java.io.*;
class ShowFile
{
}
 \end{filecontents*}

 \documentclass[svgnames]{report}
  \usepackage{tikz}
  \usepackage{verbatim}
  \usepackage{kpfonts}
  \usepackage[explicit]{titlesec}
  \usetikzlibrary{calc}
  \usetikzlibrary{shadows}
   \usetikzlibrary{shadows.blur}
  \usetikzlibrary{matrix}
   \usetikzlibrary{shapes,snakes}
   \usetikzlibrary{shapes.geometric, arrows}
  \usepackage{listings}
   \usepackage{showframe}

   \definecolor{codegreen}{rgb}{0,0,0}
   \definecolor{codegray}{rgb}{0,0,0}
   \definecolor{codepurple}{rgb}{0,0,0}
   %\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
   \definecolor{backcolour}{rgb}{1,1,1}
   \lstdefinestyle{mystyle}{
   backgroundcolor=\color{backcolour},   
   commentstyle=\color{codegreen},
   numberstyle=\tiny\color{codegray},
   stringstyle=\color{codepurple},
   basicstyle=\normalsize\ttfamily,
   breakatwhitespace=false,         
   breaklines=true,                 
   captionpos=b,                    
   keepspaces=true,                 
   numbers=none,                    
   numbersep=5pt,                  
   showspaces=false,                
   showstringspaces=false,
   showtabs=false,                  
   tabsize=4, 
   lineskip=.1cm
  }



\lstset{style=mystyle}

 \begin{document}
      \tikzstyle{Container} = [draw=none, fill=gray, thick,
      rectangle, rounded corners, inner sep=10pt, inner ysep=20pt]
      \tikzstyle{ProgramBox} = [draw=black, fill=white,thick,
      rectangle, rounded corners, inner sep=10pt, inner ysep=20pt,drop    shadow={color=black}
      ]

     \tikzstyle{ExampleBox} = [draw=black, fill=white, thick,
      rectangle, rounded corners, inner sep=10pt, inner ysep=20pt,drop     shadow={color=black}]

{\centering  
        \begin{tikzpicture}
        \label{num}
        \node [Container](container){   
                    \begin{tikzpicture}
                    \node [ProgramBox] (pbox){%
                        \begin{minipage}{\dimexpr\textwidth-40pt\relax}
                        \hrule width \hsize height 1pt


                        \lstinputlisting[language=Java]{FristProgram.java}
                        \hrule width \hsize \kern 1mm \hrule width \hsize height 1pt

                        \end{minipage}      
                    };      
                    \end{tikzpicture}
        };
        \node [ExampleBox, anchor=north west] at (container.south west) {
                \begin{minipage}{0.3\textwidth}
                    this\\
                    is \\
                    the \\
                    first \\
                    example                     
                \end{minipage}
        };  

        \node [ExampleBox, anchor=north east] at (container.south east) {
                \begin{minipage}{0.3\textwidth}
                    this\\
                    is \\
                    the \\
                    second \\
                    example \\
                    which \\
                    is \\
                    larger\\
                    than \\
                    first\\
                    example                 
                \end{minipage}
            };
        \end{tikzpicture}%
\par}

   \end{document}
  • I'm not sure what you mean by that. If you try and set the “second example” with \ttfamily and the same text as the “first example”, you'll see that the heights are essentially the same. You can try basicstyle=\linespread{0.9}\normalfont\ttfamily, if you so prefer. – egreg Sep 19 '18 at 10:56

2 Answers2

4

The lineskip= parameter in your \lstdefinestyle controls this. Try changing it to lineskip=-.1cm, for example:

enter image description here

With the original setting, lineskip=.1cm, it gives this:

enter image description here

3

just show, that Steven B. Segletes's solution also work with my answer on your previous question:

\begin{filecontents*}{FristProgram.java}
import java.io.*;
class ShowFile
{
}
 \end{filecontents*}

 \documentclass[svgnames]{report}
\usepackage{tikz}
\usetikzlibrary{arrows,
                backgrounds,
                fit,
                positioning,
                shadows}
\usepackage{listings}
\usepackage{graphicx}

\definecolor{codegreen}{rgb}{0,0,0}
   \definecolor{codegray}{rgb}{0,0,0}
   \definecolor{codepurple}{rgb}{0,0,0}
   %\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
   \definecolor{backcolour}{rgb}{1,1,1}
   \lstdefinestyle{mystyle}{
   backgroundcolor=\color{backcolour},
   commentstyle=\color{codegreen},
   numberstyle=\tiny\color{codegray},
   stringstyle=\color{codepurple},
   basicstyle=\normalsize\ttfamily,
   breakatwhitespace=false,
   breaklines=true,
   captionpos=b,
   keepspaces=true,
   numbers=none,
   numbersep=5pt,
   showspaces=false,
   showstringspaces=false,
   showtabs=false,
   tabsize=4,
   lineskip=-2ex % <---
  }



\lstset{style=mystyle}
\begin{document}

\begin{center}
    \begin{tikzpicture}[
   node distance = 8pt and 24pt,
      box/.style = {rectangle, rounded corners, draw, thick, fill=white,
                    text width=#1, inner xsep=10pt, inner ysep=20pt,
                    drop shadow={color=black}
                    },
container/.style = {rectangle, rounded corners, fill=gray,
                    text width=\linewidth, inner xsep=10pt, inner ysep=20pt
                    }
                        ]
%\label{num}
\node [box=0.95\linewidth,align=center] (list)
    {
    \hrule width \hsize height 1pt
    \lstinputlisting[language=Java]{FristProgram.java}
    \hrule width \hsize \kern 1mm 
    \hrule width \hsize height 1pt
    };
\scoped[on background layer]
    \node [container, fit=(list)] {};
%\label{num}
\node [box=0.3\linewidth, below right=of list.south west]
    {this\\
     is \\
     the \\
     first \\
     example};
\node [box=0.3\linewidth,below left=of list.south east]
    {this\\
     is \\
     the \\
     second \\
     example \\
     which \\
     is \\
     larger\\
     than \\
     first\\
     example};
     \end{tikzpicture}%
\end{center}
\end{document}

enter image description here

Zarko
  • 296,517