2

I want to define a code block macro that is used like this:

\begin{codeblock}[bash]
  # This is a comment
  sudo apt-get install texlive
\end{codeblock}

or even better:

\codeblock[bash]{
  # This is a comment
  sudo apt-get install texlive
}

that will be styled like the following: Example Rendering

However the current example uses multiple layered environments:

\begin{myquote}
    \begin{lstlisting}[language=Bash]
        # this is a comment
        sudo apt-get install texlive
    \end{lstlisting}
\end{myquote}

where myquote was defined using \newtcolorbox. I know that there is a command that defines a new environment. that doesn't work witk lstlisting. There is a command that defines a custom lstenvironment, however that does not work with the tcolorbox.

Full Latex Example

\documentclass[]{article}
\usepackage[utf8]{inputenc}
\title{Developers \& Operations Handbook}

\usepackage{environ}
\usepackage{listings}
\usepackage[most]{tcolorbox}
\usepackage{xcolor}

\definecolor{background}{RGB}{240,242,245}
\definecolor{primary}{RGB}{0,170,255}
\definecolor{success}{RGB}{60,220,100}
\definecolor{danger}{RGB}{240,85,110}
\definecolor{warning}{RGB}{255,170,70}

\newtcolorbox{myquote}[1][]{%
    enhanced,breakable,colframe=background,
    colback=background,notitle,size=fbox,
    arc=0mm,outer arc=0mm,coltitle=primary,
    fonttitle=\bfseries\large,boxsep=5mm,
    left=0mm,right=0mm,
    borderline west={1mm}{0pt}{primary},
    before={\noindent},
    segmentation style={solid, primary!0},
}

\begin{document}


\begin{myquote}
    Normal Quotes do work
\end{myquote}

% This is how I can do code at the moment
\begin{myquote}
    \begin{lstlisting}[language=Bash]
    # this is a bash comment
    sudo apt-get install texlive-base
    \end{lstlisting}
\end{myquote}

% this is how I want do do code in the future
% \codeblock{
%   # this is a bash comment
%   sudo apt-get install texlive-base
% }

% == OR ==
% \begin{codeblock}[Bash]
% # this is a bash comment
% sudo apt-get install texlive-base
% \end{codeblock}

% This does not work (https://tex.stackexchange.com/questions/86705/lstlisting-in-a-newenvironment)
% \newenvironment{codeblock}[1][Bash]
% {\begin{myquote}\begin{lstlisting}[language=#1]}
% {\end{lstlisting}\end{myquote}}

% This also doesn't work
% \lstnewenvironment{codeblock}[1]
% {\begin{myquote}}}
% {\end{myquote}}


\end{document}
Tobi
  • 255

1 Answers1

1

You can always use a tcblisting box with an aspect similar to myquote:

\documentclass[]{article}
\usepackage[utf8]{inputenc}
\title{Developers \& Operations Handbook}

\usepackage{environ}
\usepackage{listings}
\usepackage[most]{tcolorbox}
\usepackage{xcolor}

\definecolor{background}{RGB}{240,242,245}
\definecolor{primary}{RGB}{0,170,255}
\definecolor{success}{RGB}{60,220,100}
\definecolor{danger}{RGB}{240,85,110}
\definecolor{warning}{RGB}{255,170,70}

\newtcolorbox{myquote}[1][]{%
    enhanced, breakable, colframe=background,
    colback=background, notitle, size=fbox,
    arc=0mm, outer arc=0mm, coltitle=primary,
    fonttitle=\bfseries\large, boxsep=5mm,
    left=0mm, right=0mm,
    borderline west={1mm}{0pt}{primary},
    before={\noindent},
    segmentation style={solid, primary!0},
}

\newtcblisting{quotelst}{%
     listing only,
    enhanced, breakable, colframe=background,
    colback=background, notitle, size=fbox,
    arc=0mm, outer arc=0mm, coltitle=primary,
    fonttitle=\bfseries\large, boxsep=5mm,
    left=0mm, right=0mm,
    borderline west={1mm}{0pt}{primary},
    before={\noindent},
    segmentation style={solid, primary!0},
    listing options={language=Bash}
}

\begin{document}

\begin{myquote}
    Normal Quotes do work
\end{myquote}

% This is how I can do code at the moment
    \begin{quotelst}
    # this is a bash comment
    sudo apt-get install texlive-base
    \end{quotelst}

\end{document}

enter image description here

Ignasi
  • 136,588
  • is it possible to parametrize the language, so that i can do \begin{quotelst}[Bash] or \begin{quotelst}[language=Bash]? – Tobi Jun 03 '19 at 16:32