3

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.

  • Can one put three minted environments directly after one another, where only the desired ones have a background? – Tom Bombadil Nov 23 '15 at 10:04
  • Thanks for the advice. I tried it out (see possible answer posted below). – Mic D Nov 23 '15 at 11:04
  • Probably you can insert some negative space between the environments, like \vspace{-3.5mm}? – Tom Bombadil Nov 23 '15 at 14:59
2

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?

  • you can work around the second problem by adding bgcolor=white to the code blocks which have no bgcolor. This also reduces the first problem. – jakun Jun 7 '17 at 18:28
  • my comment referred to minted version 1.7. Regarding minted 2.4 please see my question here. – jakun Jun 24 '17 at 13:21

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.