minted
minted copied to clipboard
Code size limit?
Hello!
I've been trying to insert a large fragment of code (1238 lines) as an appendix to my LaTeX document. I've tried both the minted
environment and inserting an external file via inputminted
. But I always get this message many times during the compilation, which of course fails:
Dimension too large.
\fb@put@frame ...p \ifdim \dimen@ >\ht \@tempboxa
I tried the options mentioned above as they are and also wrapping them in a custom environment using \newenvironment{code}{\captionsetup{type=listing}}{}
to allow captioning, but to no avail. I can attach whatever additional information you may need.
Is there a hard limit on the file size or maybe I'm doing something wrong? Is there any workaround to insert large pieces of code with syntax highlighting?
Thank you in advance!
Are you using bgcolor
to set a background color? It looks like there may be a length limitation when bgcolor
is used, in which case using a package like tcolorbox
, possibly with breakable=unlimited
, may be a better option if you need a background color.
Yes! I'm using bgcolor
. I commented out the bgcolor
style option I had specified at the preamble and the problem disappeared. I'll check the tcolorbox
option then, hope it is an easy replacement... Thank you!
Hi! Coming here from #257, this is my first bout with TeX, so... How exactly do I implement the solution provided here? I had an enviroment \newminted[code]{js}{bgcolor=codebg,...}
, which was crashing, so I attempted to change it using the mdframed
package to
\newminted[mintjs]{js}{...}
\newenviroment{code}%
{\VerbatimEnvironment\begin{mdframed}[backgroundcolor=codebg]\begin{mintjs}}%
{\end{mintjs}\end{mdframed}}
and now it just runs indefinitely until it presumably runs out of memory and crashes.
Right, ok, realised as I was writing that last comment, so it turns out when people say "The verbatim environment doesn't use TeX verbs and just looks for \end{minted}
specifically", it means I actually have to use \end{minted}
in the environtment. Neat.
This works:
\newenvironment{code}%
{\VerbatimEnvironment\begin{mdframed}[backgroundcolor=codebg]\begin{minted}{js}}%
{\end{minted}\end{mdframed}}
EDIT: Just kidding, after some magic number of lines, it will run for like 30 minutes until it runs out of memory, even with the above change.
@Myrdden
mdframed
seems unmaintained, for there are many unsolved bugs and the last time it bumped version, from v1.9d to v2.0, happens in 2014 and was not uploaded to CTAN.
Hence I will provide a tcolorbox
example here:
\documentclass{article}
\usepackage{lipsum}
\usepackage{tcolorbox}
\tcbuselibrary{minted}
\newtcblisting{code}[1]{%
% geometry
size=minimal, after=, breakable
% content
listing only,
% color
colback=gray!20,
% minted
minted language=js,
% optional extra
#1}
\begin{document}
\lipsum[55]
\begin{code}{}
var x, y, z;
\end{code}
\lipsum[55]
\end{document}
And here is another approach which adds background color by redefining \FancyVerbFormatLine
.
\documentclass{article}
\usepackage{lipsum}
\usepackage{minted}
% config global fvextra options
\fvset{
breaklines,
% clear before and after skip, see https://github.com/gpoore/minted/issues/256
% listparameters=\setlength{\topsep}{0pt}\setlength{\partopsep}{0pt}
}
% add background color
\def\FancyVerbFormatLine#1{%
\colorbox{gray!20}{\strut #1}%
}
\makeatletter
% backup and restore \fboxsep
\newlength{\fboxsep@backup}
\BeforeBeginEnvironment{minted}
{\setlength{\fboxsep@backup}{\fboxsep}\setlength{\fboxsep}{0pt}}
\AfterEndEnvironment {minted}
{\setlength{\fboxsep}{\fboxsep@backup}}
\makeatother
\begin{document}
\lipsum[55]
\begin{minted}[autogobble]{js}
var x, y, z;
\end{minted}
\lipsum[55]
\end{document}
@muzimuzhi Yes! That works perfectly (both do, but I went with the first as it didn't also overwrite \mintinline
), thank you so much.