我有一个公式,使用 TikZ 图片叠加注释:
\documentclass{memoir}
\usepackage{calc}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{tikzmark,calc,fit,shapes.geometric}
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendisse lectus tortor, dignissim sit amet, adipiscing nec, ultricies sed, dolor.
\[ \tikzmarknode{A}{3} + \frac{\tikzmarknode{Bn}{\makebox[\widthof{7}][c]{1}}\hfill}{
\tikzmarknode{Bd}{7} + \dfrac{\tikzmarknode{Cn}{\makebox[\widthof{15}][c]{1}}\hfill}{
\tikzmarknode{Cd}{15} + \dfrac{\tikzmarknode{Dn}{1}\hfill}{
\tikzmarknode{Dd}{1} + \dfrac{\tikzmarknode{En}{\makebox[\widthof{292}][c]{1}}\hfill}{
\tikzmarknode{Ed}{292} + \tikzmarknode{F}{\dfrac{1}{1}}}}}} \]
\begin{tikzpicture}[remember picture, overlay]
\node[fill=gray!30,blend mode=darken,rectangle,inner sep=1pt,fit=(A)] (Abox){};
\node[fill=gray!30,blend mode=darken,rectangle,inner sep=1pt,fit=(Bn) (Bd)] (Bbox){};
\node[fill=gray!30,blend mode=darken,rectangle,inner sep=1pt,fit=(Cn) (Cd)] (Cbox){};
\node[fill=gray!30,blend mode=darken,rectangle,inner sep=1pt,fit=(Dn) (Dd)] (Dbox){};
\node[fill=gray!30,blend mode=darken,rectangle,inner sep=1pt,fit=(En) (Ed)] (Ebox){};
\node[fill=gray!30,blend mode=darken,rectangle,inner sep=1pt,fit=(F)] (Fbox){};
\draw (A.south) node[below=2pt] {\footnotesize $A$};
\draw (Bd.south) node[below=2pt] {\footnotesize $B$};
\draw (Cd.south) node[below=2pt] {\footnotesize $C$};
\draw (Dd.south) node[below=2pt] {\footnotesize $D$};
\draw (Ed.south) node[below=2pt] {\footnotesize $E$};
\draw (F.south) node[below=2pt] {\footnotesize $F$};
\end{tikzpicture}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendisse lectus tortor, dignissim sit amet, adipiscing nec, ultricies sed, dolor.
\end{document}
正如预期的那样,文本忽略了覆盖,并且 F 填充了公式后的正常间距。如何让 LaTeX 将注释视为公式边界框的一部分,从而在字母 F 下方添加所需的空间?
如果我tikzpicture
不这样做overlay
,它就无法按预期工作。
首次尝试(无效)
根据 Qrrbrbirlbel 的建议,我创建了一个 tikzpicture,它可以完成所有事情,包括公式。但是,不仅注释和公式的位置不同,而且整个东西需要 3 页。
\documentclass{memoir}
\usepackage{calc}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{tikzmark,calc,fit,shapes.geometric}
\begin{document}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
\noindent\begin{tikzpicture}
\node (0,0) {$\subnode{A}{3} + \dfrac{\subnode{Bn}{\makebox[\widthof{7}][c]{1}}\hfill}{
\subnode{Bd}{7} + \dfrac{\subnode{Cn}{\makebox[\widthof{15}][c]{1}}\hfill}{
\subnode{Cd}{15} + \dfrac{\subnode{Dn}{1}\hfill}{
\subnode{Dd}{1} + \dfrac{\subnode{En}{\makebox[\widthof{292}][c]{1}}\hfill}{
\subnode{Ed}{292} + \subnode{F}{\dfrac{1}{1}}}}}}$};
\node[fill=gray!30,blend mode=darken,rectangle,inner sep=1pt,fit=(A)] (Abox){};
\node[fill=gray!30,blend mode=darken,rectangle,inner sep=1pt,fit=(Bn) (Bd)] (Bbox){};
\node[fill=gray!30,blend mode=darken,rectangle,inner sep=1pt,fit=(Cn) (Cd)] (Cbox){};
\node[fill=gray!30,blend mode=darken,rectangle,inner sep=1pt,fit=(Dn) (Dd)] (Dbox){};
\node[fill=gray!30,blend mode=darken,rectangle,inner sep=1pt,fit=(En) (Ed)] (Ebox){};
\node[fill=gray!30,blend mode=darken,rectangle,inner sep=1pt,fit=(F)] (Fbox){};
\draw (A.south) node[below=2pt] {\footnotesize $A$};
\draw (Bd.south) node[below=2pt] {\footnotesize $B$};
\draw (Cd.south) node[below=2pt] {\footnotesize $C$};
\draw (Dd.south) node[below=2pt] {\footnotesize $D$};
\draw (Ed.south) node[below=2pt] {\footnotesize $E$};
\draw (F.south) node[below=2pt] {\footnotesize $F$};
\end{tikzpicture}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
\end{document}
第二次尝试(几乎成功)
我找到了一个几乎可行的解决方案,即调整覆盖图片的边界框。但是,下面的空间仍然太小:
\documentclass{memoir}
\usepackage{calc}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{tikzmark,calc,fit,shapes.geometric}
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendisse lectus tortor, dignissim sit amet, adipiscing nec, ultricies sed, dolor.
\[ \tikzmarknode{A}{3} + \dfrac{\tikzmarknode{Bn}{\makebox[\widthof{7}][c]{1}}\hfill}{
\tikzmarknode{Bd}{7} + \dfrac{\tikzmarknode{Cn}{\makebox[\widthof{15}][c]{1}}\hfill}{
\tikzmarknode{Cd}{15} + \dfrac{\tikzmarknode{Dn}{1}\hfill}{
\tikzmarknode{Dd}{1} + \dfrac{\tikzmarknode{En}{\makebox[\widthof{292}][c]{1}}\hfill}{
\tikzmarknode{Ed}{292} + \tikzmarknode{F}{\dfrac{1}{1}}}}}} \]%
\begin{tikzpicture}[remember picture, overlay]
\path[use as bounding box] (F.south west) rectangle (F.north east);
\node[fill=gray!30,blend mode=darken,rectangle,inner sep=1pt,fit=(A)] (Abox){};
\node[fill=gray!30,blend mode=darken,rectangle,inner sep=1pt,fit=(Bn) (Bd)] (Bbox){};
\node[fill=gray!30,blend mode=darken,rectangle,inner sep=1pt,fit=(Cn) (Cd)] (Cbox){};
\node[fill=gray!30,blend mode=darken,rectangle,inner sep=1pt,fit=(Dn) (Dd)] (Dbox){};
\node[fill=gray!30,blend mode=darken,rectangle,inner sep=1pt,fit=(En) (Ed)] (Ebox){};
\node[fill=gray!30,blend mode=darken,rectangle,inner sep=1pt,fit=(F)] (Fbox){};
\draw (A.south) node[below=2pt] {\footnotesize $A$};
\draw (Bd.south) node[below=2pt] {\footnotesize $B$};
\draw (Cd.south) node[below=2pt] {\footnotesize $C$};
\draw (Dd.south) node[below=2pt] {\footnotesize $D$};
\draw (Ed.south) node[below=2pt] {\footnotesize $E$};
\draw (F.south) node[below=2pt] {\footnotesize $F$};
\end{tikzpicture}%
\noindent Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendisse lectus tortor, dignissim sit amet, adipiscing nec, ultricies sed, dolor.
\end{document}
答案1
我使用幻影\underset
来增加方程的高度。它是通过\edenominator
最后一个分数上的命令而不是 来调用的\denominator
。
我试图简化文件中的代码并使其统一,但总的来说,它几乎是你的初始代码。
图中,该方程出现了两次。第一次带有附加垂直距离;第二次没有附加垂直距离,以便进行比较。
代码
\documentclass{memoir}
\usepackage{calc}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{tikzmark, calc, fit, shapes.geometric}
\begin{document}
\newcommand{\numerator}[3]{\tikzmarknode{#2n}{\makebox[\widthof{#3}][c]{#1}}\hfill}
\newcommand{\denominator}[2]{\tikzmarknode{#2d}{#1}}
\newcommand{\edenominator}[2]{\underset{\phantom{.}}{\tikzmarknode{#2d}{#1}}}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non
risus. Suspendisse lectus tortor, dignissim sit amet,
ultricies sed, dolor.
\[
\tikzmarknode{A}{3}
+ \dfrac{\numerator{1}{B}{7}}{\denominator{7}{B}
+ \dfrac{\numerator{1}{C}{15}}{\denominator{15}{C}
+ \dfrac{\numerator{1}{D}{1}}{\denominator{1}{D}
+ \dfrac{\numerator{1}{E}{292}}{\denominator{292}{E}
+ \dfrac{\numerator{1}{F}{1}}{\edenominator{1}{F}}}}}}
\]
\begin{tikzpicture}[remember picture, overlay,
a/.style={fill=gray!30, blend mode=darken, rectangle, inner sep=1pt},
a-label/.style={label={[scale=.8, label distance=2pt] 270:$#1$}}]
\node[a, fit=(A), a-label={A}] (Abox) {};
\node[a, fit=(Bn) (Bd), a-label={B}] (Bbox) {};
\node[a, fit=(Cn) (Cd), a-label={C}] (Cbox) {};
\node[a, fit=(Dn) (Dd), a-label={D}] (Dbox) {};
\node[a, fit=(En) (Ed), a-label={E}] (Ebox) {};
\node[a, fit=(Fn) (Fd), a-label={F}] (Fbox) {};
\end{tikzpicture}
\noindent Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed
non risus. Suspendisse lectus tortor, dignissim sit amet, adipiscing
nec, ultricies sed, dolor.
\vspace{30pt}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed
non risus. Suspendisse lectus tortor, dignissim sit amet, adipiscing
nec, ultricies sed, dolor.
\[
\tikzmarknode{A}{3}
+ \dfrac{\numerator{1}{B}{7}}{\denominator{7}{B}
+ \dfrac{\numerator{1}{C}{15}}{\denominator{15}{C}
+ \dfrac{\numerator{1}{D}{1}}{\denominator{1}{D}
+ \dfrac{\numerator{1}{E}{292}}{\denominator{292}{E}
+ \dfrac{\numerator{1}{F}{1}}{\denominator{1}{F}}}}}}
\]
\begin{tikzpicture}[remember picture, overlay,
a/.style={fill=gray!30, blend mode=darken, rectangle, inner sep=1pt},
a-label/.style={label={[scale=.8, label distance=2pt] 270:$#1$}}]
\node[a, fit=(A), a-label={A}] (Abox) {};
\node[a, fit=(Bn) (Bd), a-label={B}] (Bbox) {};
\node[a, fit=(Cn) (Cd), a-label={C}] (Cbox) {};
\node[a, fit=(Dn) (Dd), a-label={D}] (Dbox) {};
\node[a, fit=(En) (Ed), a-label={E}] (Ebox) {};
\node[a, fit=(Fn) (Fd), a-label={F}] (Fbox) {};
\end{tikzpicture}
\noindent Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed
non risus. Suspendisse lectus tortor, dignissim sit amet, adipiscing
nec, ultricies sed, dolor.
\end{document}