4

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}

Enter image description here

WinnieNotThePooh
  • 3,008
  • 1
  • 7
  • 14
  • One hacky idea (which I couldn’t get to work myself): Inside an environment, locally redefine the commands \begin and \end to do the usual thing, unless their argument is document, in which case they should simply do nothing. Also redefine \documentclass and \usepackage to 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
  • Have a look at the Multi-file LaTeX projects. While not 100% your answer, it might bepossible to adapt other files and simply input them to the main file. – Celdor Dec 31 '22 at 09:39
  • Thank You all for your answers! – WinnieNotThePooh Dec 31 '22 at 09:44

2 Answers2

7

One possibility: the tcolorbox package

For easier examples, it can directly display the output.

For full documents, including a documentclass, packages etc., this will internally produce a pdf of the output and include the pdf. As this is a vector graphic, it shouldn't increase the document size too much (needs to be compiled with shell-escape enabled).

% !TeX program = txs:///arara
% arara: pdflatex: {synctex: on, interaction: nonstopmode, shell: yes}

\documentclass[12pt]{article} \usepackage[most]{tcolorbox} \usepackage{mathtools} \usepackage{chemmacros}

\tcbset{sidebyside}

\begin{document}

\begin{tcblisting}{} \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{tcblisting}

\begin{tcblisting}{ listing and comment, pdf comment, freeze pdf, compilable listing, run pdflatex, } \documentclass[12pt]{article} \begin{document} qqq \end{document} \end{tcblisting}

\end{document}

enter image description here

0

Also as a variant

\documentclass{article}
\usepackage{showexpl}
\begin{document}
\begin{LTXexample}
$1+1=2$
\end{LTXexample}
\end{document}

enter image description here

WinnieNotThePooh
  • 3,008
  • 1
  • 7
  • 14