Using big braces to show parts of code

You don't actually need any packages for this:

enter image description here

\documentclass{article}
\begin{document}

\begin{tabular}{rl} 
preamble&$\left\{\begin{minipage}{8cm}\begin{verbatim}
\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[margin=25mm]{geometry}
\usepackage{lmodern}
\usepackage{amsmath}
\begin{document}
\end{verbatim}\end{minipage}\right.$\\
body&$\left\{\begin{minipage}{8cm}\begin{verbatim}
Hello World from \LaTeX !
 $$e^x$$% don't use $$ in latex!!
That's some cool math, and quite honestly a bad example.
\end{document}
\end{verbatim}\end{minipage}\right.$
\end{tabular}
\end{document}

Here is an attempt with tikzmark and listings:

\documentclass[]{article}

\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,calc}
\usepackage{listings}

\definecolor{maincs}{RGB}{255,0,0}
\definecolor{secondarycs}{RGB}{255,179,246}

\lstset{
language=[LaTeX]TeX,
xleftmargin=2cm,
escapeinside={*@}{@*},
basicstyle=\ttfamily\small,
columns=fullflexible,
breaklines=true,
texcsstyle=*\color{maincs},
texcs={documentclass,begin,end,chapter,section,subsection,label,alpha},
moretexcs=[2]{usepackage,input},
texcsstyle=*[2]{\color{secondarycs!80!black}},
}

\newcommand{\tikzmark}[1]{%
 \tikz[baseline=-0.55ex,overlay,remember picture] \node[inner sep=0pt,] (#1) {\vphantom{T}};}

\newcommand{\braced}[3]{%
  \begin{tikzpicture}[overlay,remember picture]
  \draw [thick,decorate,decoration={brace,raise=1ex,amplitude=4pt},blue] (#2.south west-|T1.south west) -- node[anchor=west,left,xshift=-1.8ex,text=olive]{#3} (#1.north west-|T1.south west);
  \end{tikzpicture}
}

 \begin{document}
 \begin{lstlisting}
 *@\tikzmark{T1}@**@\tikzmark{P1}@*\documentclass[12pt]{article}
 \usepackage[utf8]{inputenc}
 \usepackage[margin=25mm]{geometry}
 \usepackage{lmodern}
 \usepackage{amsmath}*@\tikzmark{P2}@*
 \begin{document}
   *@\tikzmark{B1}@*Hello World from \LaTeX !
    $$e^x$$
   That's some cool math, and quite honestly a bad example.*@\tikzmark{B2}@*
 \end{document}
\end{lstlisting}
\braced{P1}{P2}{Preamble}
\braced{B1}{B2}{Body}

\end{document}

enter image description here

Tags:

Braces