I am writing a book. There are plenty of different examples of codes and on the other side of a page it displays the result of code. Sometimes I cannot compile my code and just put an image as a result of code, despite the fact that I compress all images as much as I can. It is a kind of wasting memory, so I would like to optimize the size of the book.
Is it possible somehow execute LaTeX code inside LaTeX code and see the final result? I expect something like this:
\documentclass[12pt]{article}
\usepackage{qqq}
\begin{document}
\begin{run_latex}
\documentclass[12pt]{article}
\usepackage{qqq}
\begin{document}
qqq
\end{document}
\end{run_latex}
\end{document}
Here is my MWE:
\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{amsmath,amssymb}
\usepackage{array,tabularx}
\usepackage[table]{xcolor}
\usepackage{color}
\usepackage{hyperref}
\usepackage{listings}
\lstset{ basicstyle=\ttfamily, columns=fullflexible,
frame=single, breaklines=true, postbreak=\mbox{\textcolor{red}{$\hookrightarrow$}\space}}
\begin{document}
\subsection{ Aligning equations inbetween text}
\begin{tabular}{l | c}
\begin{minipage}[m]{0.4\textwidth}
RESULT OF EXECUTED CODE\textbf{}
\end{minipage}
& \begin{minipage}[m]{0.5\textwidth}
\renewcommand\textminus{\mbox{-}}
\begin{lstlisting}[basicstyle=\ttfamily\footnotesize]
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{alignat}{2}
\intertext{Photochemical:}
K_{UV} &: M[1]& &\ch{-> M^{}}[1]
\intertext{Catalyzed:}
K_I &: I& &\ch{->} 2R \
K_S &: R + M [1]& &\ch{-> RM^{}}[1]
\end{alignat}
\end{document}
\end{lstlisting}
\end{minipage}
\end{tabular}
\end{document}



\beginand\endto do the usual thing, unless their argument isdocument, in which case they should simply do nothing. Also redefine\documentclassand\usepackageto do nothing. Then everything should work, as long as the relevant packages have already been loaded in the preamble. – Gaussler Dec 30 '22 at 18:33