1

I am trying to reproduce a macro in latex that would allow to insert code in this format: enter image description here So there is a header with listing number that is starting from the left side of the document. It is between two lines. Next is the source and only after the code listing starts. So far i have something like this:

\newcommand{\insertCode}[4]{%
Źródło: Na podstawie \cite{#4}
\lstinputlisting[language=#2,caption={#3},label=#1]{#1}
}

And I run it like this:

\insertCode{kody/testjava.jar}
           {Java}
           {Test java listing}
           {oprWlasne}

The problem is my header does not start on the left side but in the middle and The source is above the listing header, instead of between the header and the code: enter image description here

My listing configuration looks like this:

% kolory
\definecolor{mygreen}{rgb}{0,0.6,0}
\definecolor{mygray}{rgb}{0.5,0.5,0.5}
\definecolor{mymauve}{rgb}{0.58,0,0.82} 

\lstset{ %
  backgroundcolor=\color{white},   % choose the background color
  basicstyle=\footnotesize,        % size of fonts used for the code
  breaklines=true,                 % automatic line breaking only at whitespace
  captionpos=t,                    % sets the caption-position to top
  commentstyle=\color{mygreen},    % comment style
  escapeinside={\%*}{*)},          % if you want to add LaTeX within your code
  keywordstyle=\color{blue},       % keyword style
  stringstyle=\color{mymauve},     % string literal style
  numbers=left,                    % line numbers on
}

Is there a way to insert the citing in between the header and the code itself? How to make the title start at the left of the page instead in the middle and change the name of the "Listing" to a custom one? I would appreciate all help.

EDIT: Here I add MWE:

% Klasa:
\documentclass[12pt]{mwart}

% Kodowanie:
\usepackage[utf8]{inputenc}

% kody
\usepackage{lipsum}
\usepackage{listings}

% kolory
\usepackage{color}
\definecolor{mygreen}{rgb}{0,0.6,0}
\definecolor{mygray}{rgb}{0.5,0.5,0.5}
\definecolor{mymauve}{rgb}{0.58,0,0.82} 

\lstset{ %
  backgroundcolor=\color{white},            % choose the background color
  basicstyle=\ttfamily\footnotesize,        % size of fonts used for the code
  breaklines=true,                          % automatic line breaking only at whitespace
  captionpos=t,                             % sets the caption-position to top
  commentstyle=\color{mygreen},             % comment style
  escapeinside={\%*}{*)},                   % if you want to add LaTeX within your code
  keywordstyle=\color{blue},                % keyword style
  stringstyle=\color{mymauve},              % string literal style
  numbers=left,                             % line numbers on
  frame=/lines,                             % frame box config
}

% Numerowanie obiektow wedlug rozdzialow
\usepackage{chngcntr}
\AtBeginDocument{\counterwithin{lstlisting}{section}}
\counterwithin{figure}{section}
\counterwithin{table}{section}

\newcommand{\insertCode}[4]{%
\lstinputlisting[language=#2,caption={#3},label=#1]{#1}
Źródło: Na podstawie \cite{#4} \\
}

% START:
\begin{document}

\insertCode{test.java}
           {Java}
           {test java code}
           {somerefference}


\end{document}

I dont know how to use example code, so I add the test.java code file as well:

class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
        for (int i = 0; i < 100; ++i) {
            System.out.println(i);
        }
    }
}
  • 1
    You're a member of TeX.SX for a while by now, but you still post only fragments as questions. Have a look on minimal working example (MWE) for improving the post – user31729 Jun 25 '16 at 9:14
  • 1
    I have added MWE and test code. Sorry for this. – Bremen Jun 25 '16 at 9:25
2

I think you're going to have to draw the caption yourself. The code below tries to do this, but note that this isn't going to support floating listings.

\documentclass[10pt]{mwart}

\usepackage{booktabs}
\usepackage{listings}

\usepackage{color}
\definecolor{mygreen}{rgb}{0,0.6,0}
\definecolor{mygray}{rgb}{0.5,0.5,0.5}
\definecolor{mymauve}{rgb}{0.58,0,0.82}

\lstset{
  backgroundcolor=\color{white},            % choose the background color
  basicstyle=\ttfamily\footnotesize,        % size of fonts used for the code
  breaklines=true,                          % automatic line breaking only at whitespace
  commentstyle=\color{mygreen},             % comment style
  escapeinside={\%*}{*)},                   % if you want to add LaTeX within your code
  keywordstyle=\color{blue},                % keyword style
  stringstyle=\color{mymauve},              % string literal style
  numbers=left,                             % line numbers on
  aboveskip=0pt,                            % no skip above
  belowskip=\smallskipamount,               % small skip below
  lineskip=1ex,                             % extra space between lines
}

\renewcommand*\lstlistingname{Kod \'zr\`od\l owy}
\renewcommand*\lstlistlistingname{Kody \'zr\'od\l owe}

\usepackage{chngcntr}
\AtBeginDocument{\counterwithin{lstlisting}{section}}
\counterwithin{figure}{section}
\counterwithin{table}{section}

\newcommand{\insertCode}[4]{%
    \par
    \begingroup
    \normalfont
    \normalsize
    \parindent=0pt
    \parskip=0pt
    \vskip\abovetopsep
    \hrule height\heavyrulewidth
    \vskip\belowrulesep
    \addtocounter{lstlisting}{1}%
    \textbf{\lstlistingname~\thelstlisting} \strut#3\strut\par
    \addtocounter{lstlisting}{-1}%
    \vskip\aboverulesep
    \hrule height\lightrulewidth
    \vskip\belowrulesep
    \strut \'Zr\'od\l o: Na podstawie~\cite{#4}\par
    \medskip
    \lstinputlisting[language=#2,caption={[#3]},label=#1]{#1}%
    \vskip\aboverulesep
    \hrule height\heavyrulewidth
    \vskip\belowrulesep
    \endgroup
}

% START:
\begin{document}

\lstlistoflistings

\section{First section}
\section{Section section}
\insertCode{test.java}
           {Java}
           {test java code}
           {somerefference}


\end{document}

enter image description here

Some things to note.

  • Even though the \insertcode macro typesets the caption itself, it also passes it as the short caption name to \lstinputlisting. This has the effect of causing it to show up with the appropriate caption in the List of Listings.
  • I deviated slightly from the image you showed when it comes to the horizontal rules. I used some macros/dimensions from the booktabs package to produce what I thought looked like better rules. In particular, the top and bottom rule slightly thicker. The three \hrules with their corresponding \vskips above and below correspond to booktabs' \toprule, \midrule, and \bottomrule. If you don't like how those look, it's easy to change. Of course, if you use booktabs for tables (and you should), this will provide a nice uniform appearance.
  • The number is produced from the counter respects the format that you've set, namely \counterwithin{lstlisting}{section}. Since \lstinputlisting itself increments the lstlisting counter, this code manually increments it prior to typesetting the caption and then decrements it immediately afterward.
  • You example image has no color or really any special formatting but your MWE contains code to set that. I kept your code.
  • There appears to be some extra space between each line. I took a guess as to what value that should be (1 ex). Some other value (including none at all) may be more appropriate for lineskip.

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.