\documentclass{standalone}
\usepackage{tikz,fvextra,minted,luacode}
\usetikzlibrary{positioning}
\directlua{ os.execute "mkdir -p examples" }

\NewDocumentCommand{\LineCount}{m}{\directlua{
	local n, f = 0, io.open("examples/#1.tex")
	for l in f:lines() do  n = n + 1  end
	tex.print( n - 1 )
}}

\newwrite\examplefile

\NewDocumentEnvironment{ShortExample}{m}{
	\VerbatimEnvironment
	\immediate\openout\examplefile=examples/#1.tex\relax
	\begin{VerbatimWrite}[writefilehandle=\examplefile]
		\documentclass{standalone}
		\begin{document}
	\end{VerbatimWrite}
	\begin{VerbatimWrite}[writefilehandle=\examplefile]}%
	{\end{VerbatimWrite}
	\begin{VerbatimWrite}[writefilehandle=\examplefile]
		\end{document}
	\end{VerbatimWrite}
	\immediate\closeout\examplefile
	\directlua{ os.execute( "cd examples ; latexmk -pdflua ".."#1"..".tex" ) }
	\edef\lc{\LineCount{#1}}
	\begin{tikzpicture}
		\node[anchor=north west, inner sep=1ex, draw] (code) {
			\begin{minipage}[t]{0.45\textwidth}
				\inputminted[autogobble=true,firstline=3,lastline=\lc]{tex}{examples/#1.tex}
			\end{minipage}
		};
		\node[anchor=north west, right=2mm of code, draw] (output) {
			\begin{minipage}[t]{0.45\textwidth}
				\includegraphics[width=\linewidth]{examples/#1.pdf}
			\end{minipage}
		};
	\end{tikzpicture}
}

\begin{document}

\begin{ShortExample}{test}
	Test
\end{ShortExample}

\end{document}
