1

Hi I have the following code

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
\usepackage{blindtext}
\usepackage{enumerate}
\geometry{paper=a4paper, left=24mm, right=23mm, top=20mm, bottom=20mm}
%Abbildungen
\usepackage{graphicx}
\usepackage[list=true,listformat=simple]{subcaption} %Mehrere Bilder in einer figure
\usepackage[justification=raggedright]{caption}
                                    %Zentrierte Beschriftungen
\usepackage{float}                  %H-Figure Typ
\usepackage[labelfont={bf,sf},font={small},%
  labelsep=space]{caption} 
\usepackage{rotating}


\usepackage{listings}
\lstset{
    basicstyle=\ttfamily\footnotesize,
    xleftmargin=.1\textwidth, 
}
\usepackage[wide]{sidecap}                                      

\usepackage{newfloat}
\DeclareFloatingEnvironment[placement={!ht},name=]{listing}

\sidecaptionvpos{listing}{c}
\makeatletter
\makeatother

    
\begin{document}
\blindtext
(\cite{pykrige}). 
\begin{SClisting}
\begin{lstlisting}[language=Python]
from traceback import print_tb
import numpy as np 
from pykrige.ok import OrdinaryKriging
from pykrige.kriging_tools import write_asc_grid
import pykrige.kriging_tools as kt
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
\end{lstlisting}
\captionsetup{style=base}
\caption{}
\label{lst:einf}
\end{SClisting}
\blindtext
\begin{SClisting}
\begin{lstlisting}[language=Python]
def load_data():
    dflm = dfl
    return dflm

def get_data(dflm):
    return {
        "lons": dflm['Longitude'].values,
        "lats": dflm['Latitude'].values,
        "values": dflm['ValueHeight'].values
    }

dflm = load_data()
base_data = get_data(dflm)
\end{lstlisting}
\captionsetup{style=base}
\caption{}
\label{lst:laden}
\end{SClisting}

\begin{figure}[H]
    \centering
    \includegraphics{Unbenannt.PNG}
    \caption{these captions should remain as they are}
    \label{fig:my_label}
\end{figure}
\end{document}

and the following output enter image description here

However, I want the numbers on the right edge to be in a plane and surrounded by a bracket. I know I have to change the \captionsetup{style=base}snipped, however I don´t know in which way

The difficulty:I have illustrations in the same work, so the defintition of the caption for the code must be such that it only affects the code and the captions of the illustrations remain as before.

1 Answer 1

1

UPDATE after a follow-up question.

This code will put the caption on the right border with the brackets.

It uses \usepackage[margincaption,leftbody]{sidecap} and defines a new caption format to position the caption and add the brackets.

c

With \captionsetup[lstlisting]{format=myformat} myformat is applied only to the lstlisting's captions.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
\usepackage{blindtext}
\usepackage{enumerate}
\geometry{paper=a4paper, left=24mm, right=23mm, top=20mm, bottom=20mm, showframe} % added showframe <<<<<<<<<<<<<<<<<<<<<<<<
%Abbildungen
\usepackage{graphicx}
\usepackage[list=true,listformat=simple]{subcaption} %Mehrere Bilder in einer figure

%Zentrierte Beschriftungen
%\usepackage{float}                  %H-Figure Typ
\usepackage{rotating}

\usepackage[labelfont={bf,sf},font={small},labelsep=space]{caption} 
\DeclareCaptionFormat{myformat}{\hspace*{\dimexpr-2\marginparsep -\marginparwidth-3ex\relax}[#1]}% added <<<<<<<<<

\captionsetup[lstlisting]{format=myformat}% apply only to lstlisting

\usepackage{listings}
\lstset{
    basicstyle=\ttfamily\footnotesize,
    xleftmargin=.1\textwidth, 
}
                 
\usepackage[margincaption,leftbody]{sidecap}  % changed <<<<<<<<<<<<<<<<<<<

\usepackage{newfloat}
\DeclareFloatingEnvironment[placement={!ht},name=]{listing}

\sidecaptionvpos{listing}{c} % in here
\begin{document}
    \blindtext
    (\cite{pykrige}). 
\begin{SClisting}
\begin{lstlisting}[language=Python]
from traceback import print_tb
import numpy as np 
from pykrige.ok import OrdinaryKriging
from pykrige.kriging_tools import write_asc_grid
import pykrige.kriging_tools as kt
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
\end{lstlisting}
    \caption{}
    \label{lst:einf}
\end{SClisting}
    \blindtext
\begin{SClisting}
\begin{lstlisting}[language=Python]
def load_data():
    dflm = dfl
    return dflm

def get_data(dflm):
    return {
        "lons": dflm['Longitude'].values,
        "lats": dflm['Latitude'].values,
        "values": dflm['ValueHeight'].values
    }

dflm = load_data()
base_data = get_data(dflm)
\end{lstlisting}
        \caption{}
        \label{lst:laden}
    \end{SClisting}

\begin{figure}[hb!]
    \centering
    \includegraphics{example-image}
    \caption{these captions should remain as they are}
    \label{fig:my_label}
\end{figure}

\end{documen
5
  • but i have illustrations, so for the code sections i only have to make the change
    – Weiss
    Sep 24 at 10:06
  • I have updated my question ;), however a big thanks for your idea
    – Weiss
    Sep 24 at 10:26
  • @Weiss Please see the updated answer. Sep 24 at 13:11
  • Thank you very much, I still want to know how I can set the distance to the right side? I am still new to latex and do not understand everything yet.
    – Weiss
    Sep 24 at 13:28
  • @Weiss Thank you for feedback! (1) Read the caption doc on how to define a new custom style (2) Try \DeclareCaptionFormat{myformat}{\hspace*{<space>} [#1]} with different negative spaces starting with 0pt (3) Google in this site \dimexpr Sep 24 at 13:43

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.