Skip to content

Instantly share code, notes, and snippets.

@leoleoasd
Last active November 30, 2021 06:45
Show Gist options
  • Save leoleoasd/c62db8e7c4c37b5efb35cf4b61c07e7f to your computer and use it in GitHub Desktop.
Save leoleoasd/c62db8e7c4c37b5efb35cf4b61c07e7f to your computer and use it in GitHub Desktop.
Run psql and capture it's output from LaTeX.
\ProvidesPackage{psql}
\RequirePackage{minted}
\RequirePackage{fancyvrb}
\RequirePackage{tcolorbox}
\def\run{\FV@Environment{}{run}}
\def\FVB@run{%
\@bsphack
\begingroup
\FV@UseKeyValues
\FV@DefineWhiteSpace
\def\FV@Space{\space}%
\FV@DefineTabOut
\def\FV@ProcessLine{\immediate\write\FV@OutFile}%
\immediate\openout\FV@OutFile temp.sql\relax
\let\FV@FontScanPrep\relax
%% DG/SR modification begin - May. 18, 1998 (to avoid problems with ligatures)
\let\@noligs\relax
%% DG/SR modification end
\FV@Scan}
\def\FVE@run{\immediate\closeout\FV@OutFile\endgroup\@esphack
\begin{tcolorbox}[title=Code Example]%
\vspace{-12pt}%
\inputminted[bgcolor=bg]{sql}{temp.sql}%
\tcbsubtitle{Result on PostgreSQL}%
% \vspace{-12pt}%
\immediate\write18{rm temp.result}%
\immediate\write18{bash -c "$(cat test.env | xargs) psql -f temp.sql > temp.result 2>&1 $()"}%
% empty $() here fixes latex workshop's code highlight.
% else there will be a single $, which will cause wrong highlight on whole tex file.
\inputminted[bgcolor=bg,breaklines=true]{text}{temp.result}%
\vspace{-12pt}%
\end{tcolorbox}
}
\DefineVerbatimEnvironment{run}{run}{}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment