Update.
Added support to leave the empty environment in the copy, removing only its contents, or to remove also the \begin...\end
pair (by default).
I programmed a LuaLaTeX solution and tried to make it flexible enough. These are the files which compose the solution:
remove-env.lua
-- remove-env.lua
omittedEnvironments = {}
omitFileSuffix = "-without"
leaveEmptyEnvs = false
function shouldOmit(line)
for i,v in ipairs(omittedEnvironments) do
if (string.find(line, "\\begin{"..v.."}")~=nil) then
return true
end
end
return false
end
function shouldResume(line)
for i,v in ipairs(omittedEnvironments) do
if (string.find(line, "\\end{"..v.."}")~=nil) then
return true
end
end
return false
end
function dumpfile()
myout = io.open(tex.jobname..omitFileSuffix..".tex", "w")
myin = io.open(tex.jobname..".tex", "r")
omitting = false
for line in myin:lines() do
if (not omitting and shouldOmit(line)) then
if (leaveEmptyEnvs) then myout:write(line.."\n") end
omitting = true
end
if (not omitting) then
myout:write(line.."\n")
end
if (omitting and shouldResume(line)) then
if (leaveEmptyEnvs) then myout:write(line.."\n") end
omitting = false
end
end
myout:close()
myin:close()
end
remove-env.tex
\directlua{dofile("remove-env.lua")}
\def\omitEnvironment#1{\directlua{table.insert(omittedEnvironments, "#1")}}
\def\omitFileSuffix#1{\directlua{omitFileSuffix="#1"}}
\def\leaveEmptyEnvs{\directlua{leaveEmptyEnvs=true}}
\def\removeEmptyEnvs{\directlua{leaveEmptyEnvs=false}}
\AtEndDocument{\directlua{dumpfile()}}
MWE.tex
\input remove-env
\documentclass{article}
\usepackage{fontspec}
\usepackage{lipsum}
\newenvironment{solution}{}{}
\omitEnvironment{solution}
\omitFileSuffix{-sans-sol}
\begin{document}\parindent0pt\parskip1em
1. \lipsum[1]\hrulefill\par
\begin{solution}
2. \lipsum[2]\hrulefill\par
\end{solution}
3. \lipsum[3]\hrulefill\par
\end{document}
This MWE defines a no-op solution
environment which acts simply as markup, but of course you can define it in a way that produces some effect in the pdf. Macro \omitEnvironment
specifies the environment you want to omit. You can use this macro several times to specify several environments, and all of them will be omitted. Macro \omitFileSuffix
specifies the suffix that will be appended to the output filename.
Run:
$ lualatex MWE.tex
And you will get two files (and all the usual auxiliar files, of course):
MWE.pdf
will be generated as usually, and all the contents (including omitted environments) will be present.
MWE-sans-sol.tex
is a copy of MWE.tex
in which all solution
environments are removed.
$ diff MWE.tex MWE-sans-sol.tex
11,13d10
< \begin{solution}
< 2. \lipsum[2]\hrulefill\par
< \end{solution}
If you want to remove only the contents of the solution but leave the empty environment, you only have to specify \leaveEmptyEnvs
at some point of MWE.tex
. In this case the diff will show:
$ diff MWE.tex MWE-sans-sol.tex
12d11
< 2. \lipsum[2]\hrulefill\par
PS: Thanks to Scott H. who suggested me not to use luatex callbacks, which was my first (and too convoluted) approach
Some fixes
Well, you were on the right track. You just change \newmint
into \newmintedfile
and, after fixing some settings in your MWE, it will compile with the -shell-escape
option passed to pdflatex
.
The following were the fixes.
- There is no
openright
option for article
class, and hence no chapter
option for minted
as this seems like your settings in your original report
file
- There was a typo in
\inputmined
. It should be \inputminted
.
Explanation of \newmintedfile
The syntax is \newmintedfile[<alias>]{<language>}{<external file>}
.
The usage is the same as that of \newmint
. If you have explicitly specified its alias, say phpcode
then you can use \phpcode{foo.php}
, otherwise, file
is appended to the language name, in this case you write \phpfile{foo.php}
. The documentation was not so good at explaining this but you can find the parallelism with the \newmint
command and their definitions.
The codes
Here is the file foo.php
that I used:
<?php
echo $_SERVER['HTTP_USER_AGENT'];
?>
And here is the foobar.php
file:
<?php
//This is a PHP comment line
/*
This is
a PHP comment
block
*/
?>
And finally the LaTeX code.
\documentclass[preview,border=5]{standalone}
%php syntax highlight
\usepackage[section]{minted}
\definecolor{mintedbackground}{rgb}{0.95,0.95,0.95}
\newmintedfile[phpcode]{php}{
bgcolor=mintedbackground,
fontfamily=tt,
linenos=true,
numberblanklines=true,
numbersep=5pt,
gobble=0,
frame=leftline,
framerule=0.4pt,
framesep=2mm,
funcnamehighlighting=true,
tabsize=4,
obeytabs=false,
mathescape=false
samepage=false, %with this setting you can force the list to appear on the same page
showspaces=false,
showtabs =false,
texcl=false,
}
\usepackage{kantlipsum}
\begin{document}
\phpcode{foo.php}
\kant[1]
\phpcode{foobar.php}
\end{document}
The output
And here is the output
Best Answer
Try
\inputminted{c}{main.c}
or generally\inputminted[options]{language}{filename}
-- see page 4 of the minted manual.