6

I am writing code form different languages in one document. Because of that it would be nice, if it is possible to display the name of the language below the rule of an lstlisting.

This is my code

\documentclass{article}

\usepackage{listings}
\usepackage{xcolor}

\lstset{
%   backgroundcolor=\color{},
    commentstyle=\color{green},
    keywordstyle=\color{blue},
    stringstyle=\color{black!50!green},
    %        identifierstyle=\color{red},
    numbers=left,
    captionpos=t,
    showstringspaces=false,
    tabsize=2,
    frame=b,
    breaklines=true,
    xleftmargin=17pt,
    framexleftmargin=17pt,
}

\begin{document}


    Much text
    \begin{lstlisting}[language=C++]
#inlcude <iostream.h>

int end = 5;

for(int i = 0; i < end; i++)
{
    std::cout << "Hello world!";
}
    \end{lstlisting}

%This should happen automaticly
%----------------------------------------------------

 \begin{flushright}
    c++
 \end{flushright}

%----------------------------------------------------

\end{document}

1 Answer 1

6

With the package etoolbox you can hook to the lstlisting environment to store the language option in a macro. Then you can add code after the environment to display the flushright environment with the language.

MWE:

\documentclass{article}

\usepackage{listings}
\usepackage{xcolor}
\usepackage{etoolbox}

\makeatletter
\AtEndEnvironment{lstlisting}{\xdef\xlang{\lst@language}}
\AfterEndEnvironment{lstlisting}{\begin{flushright}\xlang\end{flushright}}
\makeatother

\lstset{
%   backgroundcolor=\color{},
    commentstyle=\color{green},
    keywordstyle=\color{blue},
    stringstyle=\color{black!50!green},
    %        identifierstyle=\color{red},
    numbers=left,
    captionpos=t,
    showstringspaces=false,
    tabsize=2,
    frame=b,
    breaklines=true,
    xleftmargin=17pt,
    framexleftmargin=17pt,
}

\begin{document}


    Much text
    \begin{lstlisting}[language=C++]
#include <iostream.h>

int end = 5;

for(int i = 0; i < end; i++)
{
    std::cout << "Hello world!";
}
    \end{lstlisting}

\begin{lstlisting}[language=bash]
echo "Hello world!"
\end{lstlisting}

\end{document}

Result:

enter image description here

2
  • Thank you for your answer. That is what I am looking for. Is it possible to reduce the distance between the environment and the language?
    – NelDav
    Mar 20, 2018 at 8:41
  • @Ahrtaler an easy workaround is to add some negative vertical space at the start of the environment: \begin{flushright}\vspace{-5mm}\xlang\end{flushright}
    – Marijn
    Mar 20, 2018 at 8:53

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.