6

I want to tune the type of tcolorbox that I'm making.

I want specifically to only draw a border on one side of the image, similar to how minted allows to draw only a border on one side by specifying option frame=leftline.

I got the following code:

\documentclass[a5paper]{article}
\usepackage[T1]{fontenc}
\usepackage[]{minted}
\usepackage[skins]{tcolorbox}
\usepackage{lineno}
\def\gobble#1{}
\renewcommand\DeleteFile[1]{}
\usepackage{xparse}
\ExplSyntaxOn
\box_new:N \l_fvrb_box
\tl_new:N \l_fvrb_tl

\RenewDocumentCommand \FancyVerbFormatLine { m }
 {
   \hbox_set:Nn \l_fvrb_box { #1 }
    \dim_compare:nNnTF { \box_wd:N \l_fvrb_box }>{ \linewidth }
      { 
       \tl_set:Nn \l_fvrb_tl { #1 }
       \fvrb_use_tl:N \l_fvrb_tl
      } 
      {%box fits
       \box_use:N \l_fvrb_box
      }
 }

\cs_new:Npn \fvrb_use_tl:N  #1
 {
  \group_begin:
   \null\hfill\vbox_set:Nn \l_fvrb_box
     {\hsize=\linewidth
      \renewcommand\thelinenumber
           {
             \ifnum\value{linenumber}=1\relax\else
                  $\rightarrow$
             \fi
           }
      \begin{internallinenumbers}
        \advance\hsize by -2em
        \hspace*{-2em}\tl_use:N #1
      \end{internallinenumbers}
     }
   \box_use:N \l_fvrb_box
  \group_end:
}
\ExplSyntaxOff
\definecolor{mintedbg}{RGB}{250,255,250}
\usepackage{etoolbox}
\BeforeBeginEnvironment{minted}{\begin{tcolorbox}[enhanced,
width=\linewidth,
enlarge top by=3pt,enlarge bottom by=3pt,
enlarge left by=3pt,enlarge right by=3pt,
frame hidden,boxrule=0pt,top=1mm,bottom=1mm,
colframe=green!30!black, colbacktitle=green!50!yellow,
coltitle=black, colback=mintedbg,
borderline={.8pt}{-0.5pt}{blue!87!green!13, sharp corners},
borderline={.9pt}{-3pt}{blue!70!green!30, sharp corners},
borderline={1pt}{-5.5pt}{blue!53!green!47, sharp corners}]}%
\AfterEndEnvironment{minted}{\end{tcolorbox}}%
\begin{document}
\begin{minted}{latex}
%Preamble
\usepackage[]{minted}
\usepackage[fancyvrb=true]{listings}
\usepackage{tcolorbox}
\usepackage{etoolbox}
\BeforeBeginEnvironment {minted} { \begin{tcolorbox} } \AfterEndEnvironment {minted} { \end{tcolorbox} }%
\usepackage{lipsum}
\end{minted}
\end{document}

Ideally, I'd want a style very similar to minted's frame=leftline, except with multiple (border-)lines as outlined in the example.

  • Rectangular corners can be obtained with arc=0mm,auto outer arc as additional options to the tcolorbox environment. – user31729 May 16 '14 at 18:27
  • @ChristianHupfer I actually just found the solution to the first question, funnily. The solution is an extra argument to borderline. Example: borderline={1pt}{-5.5pt}{blue!53!green!47, sharp corners}. :-) – 1010011010 May 16 '14 at 18:32
  • Let me know if my answer gives the style for the lines that you want, since I am not familiar with the result of the leftline option in minted. Any required change can be easily made. – Gonzalo Medina May 16 '14 at 19:18
6

One option:

\documentclass[a5paper]{article}
\usepackage[T1]{fontenc}
\usepackage[]{minted}
\usepackage[skins]{tcolorbox}
\usepackage{lineno}
\def\gobble#1{}
\renewcommand\DeleteFile[1]{}
\usepackage{xparse}
\ExplSyntaxOn
\box_new:N \l_fvrb_box
\tl_new:N \l_fvrb_tl

\RenewDocumentCommand \FancyVerbFormatLine { m }
 {
   \hbox_set:Nn \l_fvrb_box { #1 }
    \dim_compare:nNnTF { \box_wd:N \l_fvrb_box }>{ \linewidth }
      { 
       \tl_set:Nn \l_fvrb_tl { #1 }
       \fvrb_use_tl:N \l_fvrb_tl
      } 
      {%box fits
       \box_use:N \l_fvrb_box
      }
 }

\cs_new:Npn \fvrb_use_tl:N  #1
 {
  \group_begin:
   \null\hfill\vbox_set:Nn \l_fvrb_box
     {\hsize=\linewidth
      \renewcommand\thelinenumber
           {
             \ifnum\value{linenumber}=1\relax\else
                  $\rightarrow$
             \fi
           }
      \begin{internallinenumbers}
        \advance\hsize by -2em
        \hspace*{-2em}\tl_use:N #1
      \end{internallinenumbers}
     }
   \box_use:N \l_fvrb_box
  \group_end:
}
\ExplSyntaxOff
\definecolor{mintedbg}{RGB}{250,255,250}
\usepackage{etoolbox}
\BeforeBeginEnvironment{minted}{%
\begin{tcolorbox}[
  width=\linewidth,
  enlarge top by=3pt,
  enlarge bottom by=3pt,
  enlarge left by=3pt,
  enlarge right by=3pt,
  frame hidden,boxrule=0pt,
  top=1mm,
  bottom=1mm,
  colframe=green!30!black, 
  colbacktitle=green!50!yellow,
  coltitle=black, 
  colback=mintedbg,
  freelance,
  arc=0pt,
  outer arc=0pt,
  boxrule=0pt,
  frame code={
    \draw[blue!87!green!13,line width=0.8pt] 
      ([xshift=-0.5pt]frame.north west) -- ([xshift=-0.5pt]frame.south west);
    \draw[blue!70!green!30,line width=0.9pt] 
      ([xshift=-3pt]frame.north west) -- ([xshift=-3pt]frame.south west);
    \draw[blue!53!green!47,line width=0.8pt] 
      ([xshift=-5.5pt]frame.north west) -- ([xshift=-5.5pt]frame.south west);
  }
]}
\AfterEndEnvironment{minted}{\end{tcolorbox}}%
\begin{document}
\begin{minted}{latex}
%Preamble
\usepackage[]{minted}
\usepackage[fancyvrb=true]{listings}
\usepackage{tcolorbox}
\usepackage{etoolbox}
\BeforeBeginEnvironment {minted} { \begin{tcolorbox} } \AfterEndEnvironment {minted} { \end{tcolorbox} }%
\usepackage{lipsum}
\end{minted}
\end{document}

The result:

enter image description here

I changed from enhanced to freelance and used frame code to draw the lines only to the left (I also set arc and outer arc to 0pt); adjust the settings for the \draw commands according to your needs.

| improve this answer | |
  • \l_fvrb_box and \l_fvrb_tl are bad names. – egreg May 16 '14 at 20:38
  • @egreg No idea. Those were in the original code and were not directly related to the question so I used them as they were. Could you please explain why and suggest better ones? – Gonzalo Medina May 16 '14 at 22:39
  • @egreg Took the code directly from the author of tcolorbox as a solution for linebreaking inside tcolorboxes. – 1010011010 May 18 '14 at 8:35
  • 1
    @1010011010 Don't propagate bad naming styles; l is the prefix for a local variable; then there should be a distinguishing “namespace prefix” (it could be ioiooiioio in your case), then the proper name; so the box should be like \l_ioiooiioio_fvrb_line_box – egreg May 18 '14 at 8:43
  • @egreg I'm propagating somebody else's creations. You seem pretty adamant on getting the naming scheme right so tex.stackexchange.com/questions/112559/… there you go.. that's where I got it from. If you still intend to contain the error? ;-) – 1010011010 May 18 '14 at 10:30

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.