gpoore / minted

minted is a LaTeX package that provides syntax highlighting using the Pygments library. Highlighted source code can be customized using fancyvrb.
1.74k stars 126 forks source link

multicols #375

Closed emeth69 closed 10 months ago

emeth69 commented 1 year ago

Dear Minted Experts, is there a way to have code formatted by minted in 2 or more columns side by side as the multicols=«number» option of the listings package? Or some workaround to have it implemented?

Thank you

muzimuzhi commented 1 year ago
\documentclass{article}
\usepackage{minted}
\usepackage{multicol}

\begin{document}
\begin{multicols}{2}
\begin{minted}[linenos]{tex}
\documentclass{article}
\usepackage{amsmath}

\begin{document}
content
\[
  a^2 + b^2 = c^2
\]
\end{document}
\end{minted}
\end{multicols}
\end{document}

image

emeth69 commented 1 year ago

Yes but this doesn't work with \inputminted inside a tcolorbox, as in

\begin{tcolorbox}[%
         unbreakable, enhanced, colback={red}, colframe={yellow}, %
         sharp corners, boxrule=1pt, enhanced jigsaw, %
         opacityback=.50, boxsep=0pt, width=\textwidth, left=3pt, %
         right=3pt, top=3pt, bottom=2pt, arc=0pt, outer arc=0pt]
         \inputminted{python}{helloworld.py}%
      \end{tcolorbox}
muzimuzhi commented 1 year ago
% !TeX TXS-program:compile = txs:///pdflatex/{%.tex} -shell-escape "%.tex"
\begin{filecontents}[noheader]{sample-doc.tex}
\documentclass{article}
\usepackage{amsmath}

\begin{document}
content
\[
  a^2 + b^2 = c^2
\]
\end{document}
\end{filecontents}

\documentclass{article}
\usepackage{minted}
\usepackage{multicol}
\usepackage{tcolorbox}

\tcbuselibrary{breakable, skins}

\begin{document}
\begin{tcolorbox}[
  unbreakable, enhanced, colback={red}, colframe={yellow},
  sharp corners, boxrule=1pt, enhanced jigsaw,
  % "opacityback=.50" changed to "opacityback=.20"
  opacityback=.20, boxsep=0pt, width=\textwidth, left=3pt,
  right=3pt, top=3pt, bottom=2pt, arc=0pt, outer arc=0pt,
  before upper=\begin{multicols}{2}, after upper={\end{multicols}} % <<< added
]
  \inputminted{tex}{sample-doc.tex}
\end{tcolorbox}
\end{document}

image

Here

\begin{tcolorbox}[before upper=\begin{multicols}{2}, after upper={\end{multicols}}]
  content
\end{tcolorbox}

is equivalent to

\begin{tcolorbox}
  \begin{multicols}{2}
    content
  \end{multicols}
\end{tcolorbox}

PS: Only environments that don't collect its content can be used this way (through options before upper and after upper).

emeth69 commented 1 year ago

Great! This works. Thank you.

goyalyashpal commented 10 months ago

@emeth69 it seems this issue is solved for you, so, can you close this :)

goyalyashpal commented 10 months ago

Only environments that don't collect its content

can you explain this a bit more?

muzimuzhi commented 10 months ago

@goyalyashpal It's hard to give a formal definition. For multicols env,

\begin{tcolorbox}[before upper=\begin{multicols}{2}, after upper={\end{multicols}}]
  content
\end{tcolorbox}

and

\begin{tcolorbox}
  \begin{multicols}{2}
    content
  \end{multicols}
\end{tcolorbox}

give the same output. But for some other environments, only the second form works and the first form may even fail to compile. Apart from verbatim-like envs, tcolorbox-based envs, amsmath multi-line display math envs, and several other commonly seen envs are in that list. See https://github.com/T-F-S/tcolorbox/issues/250 for related discussions.