[Tex/LaTex] Background color for a range of lines in Minted

backgroundsminted

I have a long piece of code that highlight with minted.

I would like to have a background only around a block of code. How can I achieve that?

I tried to use something like escapeinside=|| to create a color box.

In this example we want to put a background to central part, between the \colorbox{green}... and \end{minipage}} commands.

\begin{minted}[linenos=true,escapeinside=||,breaklines=true]{cpp}
...
    if (is_continuous<T>::value && value.size() == 0)
        ar.write(path, static_cast<typename scalar_type<std::vector<T, A> >::type const *>(NULL), std::vector<std::size_t>());
    else if (is_continuous<T>::value) {
        |\colorbox{green}{\begin{minipage}{\textwidth}|
        std::vector<std::size_t> extent(get_extent(value));
        std::copy(extent.begin(), extent.end(), std::back_inserter(size));
        std::copy(extent.begin(), extent.end(), std::back_inserter(chunk));
        std::fill_n(std::back_inserter(offset), extent.size(), 0);
        ar.write(path, get_pointer(value), size, chunk, offset);
        |\end{minipage}}|
    } else if (value.size() == 0)
        ar.write(path, static_cast<int const *>(NULL), std::vector<std::size_t>());
    else if (is_vectorizable(value)) {
...
\end{minted}

However, this does not compile, I think because the escapeinside command cannot leave open blocks.

Any idea for achieving the desired behavior?

thanks.

Best Answer

One possible solution (thanks Tom Bombadil) would split the code in more minted blocks. However, this also shows some problem. (see below)

What I tried is the following code

\begin{minted}[linenos=true,fontsize=\footnotesize,breaklines=true]{cpp}
    ...
    if (is_continuous<T>::value && value.size() == 0)
        ar.write(path, static_cast<typename scalar_type<std::vector<T, A> >::type const *>(NULL), std::vector<std::size_t>());
    else if (is_continuous<T>::value) {
\end{minted}
\begin{minted}[linenos=true,fontsize=\footnotesize,breaklines=true,firstnumber=last,bgcolor=YellowGreen]{cpp}
        /* BEGIN BLOCK 1 */
        std::vector<std::size_t> extent(get_extent(value));
        std::copy(extent.begin(), extent.end(), std::back_inserter(size));
        std::copy(extent.begin(), extent.end(), std::back_inserter(chunk));
        std::fill_n(std::back_inserter(offset), extent.size(), 0);
        ar.write(path, get_pointer(value), size, chunk, offset);
        /* END BLOCK 1 */
\end{minted}
\begin{minted}[linenos=true,fontsize=\footnotesize,breaklines=true,firstnumber=last]{cpp}
    } else if (value.size() == 0)
        ar.write(path, static_cast<int const *>(NULL), std::vector<std::size_t>());
    else if (is_vectorizable(value)) {
    ...
\end{minted}

which results in enter image description here

Here the problems are:

  1. The space separation between blocks
  2. The numbers have a different offset when there is a background.

Is it possible to work around these issues?