在 TeXbook 第 428 页,Knuth 使用大竖括号(通过命令\hidebrace
,但我找不到任何相关文档)来显示字体的不同部分。我希望使用类似的技术在 LaTeX 中实现以下效果:
/ \documentclass[12pt]{article}
| \usepackage[utf8]{inputenc}
preamble < \usepackage[margin=25mm]{geometry}
| \usepackage{lmodern}
\ \usepackage{amsmath}
\begin{document}
/ Hello World from \LaTeX !
body < $$e^x$$
\ That's some cool math, and quite honestly a bad example.
\end{document}
显然,我希望让括号和左侧的文本看起来更美观(而不是逐字逐句)。有什么想法吗?
答案1
tikzmark
以下是使用 和的尝试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}
答案2
实际上你并不需要任何包:
\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}
答案3
需要说明的是,\hidebrace 没有定义或记录,因为它没有被使用。它只是模板,所以从未扩展过。表中只有两列;&\hidebrace{#}\hfil 可以删除而不会导致错误(我刚试过)。我想这是早期(TeX78?)版本的表格的遗留问题。明年我们会向 Knuth 报告此事。