我想在我的方程周围创建特殊的框架。这个框架应该遵循黄金比例规则。这意味着它的宽度除以它的高度应该等于1.61803398875。
如果可能的话,代码将适应选择长的边。例如,对于一个长列向量,其高度将是其宽度的 1.61803398875 倍。
我在本论坛的另一个问题中发现了以下代码(这里)。它使用tikz
但我不知道如何修改它以获得围绕我的方程的黄金比例框架......
\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{shapes}
\makeatletter
\newdimen\@myBoxHeight%
\newdimen\@myBoxDepth%
\newdimen\@myBoxWidth%
\newdimen\@myBoxSize%
\newcommand{\SquareBox}[2][]{%
\settoheight{\@myBoxHeight}{#2}% Record height of box
\settodepth{\@myBoxDepth}{#2}% Record depth of box
\settowidth{\@myBoxWidth}{#2}% Record width of box
\pgfmathsetlength{\@myBoxSize}{max(\@myBoxWidth,(\@myBoxHeight+\@myBoxDepth))}%
\tikz \node [shape=rectangle, shape aspect=1,draw=red,inner sep=2\pgflinewidth, minimum size=\@myBoxSize,#1] {#2};%
}%
\makeatother
\begin{document}
\SquareBox{I}
\SquareBox{y}
\SquareBox[thick, dashed]{long text}
\SquareBox[draw=blue]{longer text}
\SquareBox[draw=blue, thick, fill=yellow]{$e = mc^2$}
\SquareBox[draw=black, thick, fill=yellow!10, rounded corners=2pt]{$\displaystyle \int_{-\dfrac{\pi}{2}}^{\dfrac{\pi}{2}} dx $}
\end{document}
提前感谢您的帮助。
答案1
卧式金盒
这里有一个 (纯 TikZ!) 通过fit
、calc
和backgrounds
TikZ 库求解,其中所有方程式都是水平对齐的:
\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{fit,calc,backgrounds}
\pgfmathsetmacro\goldenratio{(1+sqrt(5))/2}
\newcommand\GoldenBox[2][]{%
\begin{tikzpicture}[baseline=(eq.base)]
\node[inner sep=0] (eq) {#2};
\begin{scope}[on background layer]
\path let \p1=(eq.south west), \p2=(eq.north east),
\n{boxw}={max(\x2-\x1,(\y2-\y1)*\goldenratio)},
\n{boxh}={\n{boxw}/\goldenratio}
in node [rectangle,fit=(eq),draw=red,inner sep=2*\pgflinewidth,
minimum width=\n{boxw},minimum height=\n{boxh},#1] {};%
\end{scope}
\end{tikzpicture}%
}
\begin{document}
\GoldenBox{I}
\GoldenBox{y}
\GoldenBox[thick, dashed]{long text}
\GoldenBox[draw=blue]{longer text}
\GoldenBox[draw=blue, thick, fill=yellow]{$e = mc^2$}
\GoldenBox[draw=black, thick, fill=yellow!10, rounded corners=2pt]
{$\displaystyle \int_{-\dfrac{\pi}{2}}^{\dfrac{\pi}{2}} dx $}
\end{document}
垂直或水平 GoldenBox
\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{fit,calc,backgrounds}
\pagestyle{empty}
\pgfmathsetmacro\goldenratio{(1+sqrt(5))/2}
\newcommand\GoldenBox[2][]{%
\begin{tikzpicture}[baseline=(eq.base)]
\node[inner sep=0] (eq) {#2};
\begin{scope}[on background layer]
\path let
\p1=(eq.south west), \p2=(eq.north east),
\n{w}={\x2-\x1}, \n{h}={\y2-\y1},
\n{boxw}={\n{w}>=\n{h}?max(\n{w},\n{h}*\goldenratio):max(\n{w},\n{h}/\goldenratio)},
\n{boxh}={\n{w}>=\n{h}?max(\n{w}/\goldenratio,\n{h}):max(\n{h}:\n{w}*\goldenratio)}
in node [rectangle,fit=(eq),draw=red,inner sep=2*\pgflinewidth,
minimum width=\n{boxw},minimum height=\n{boxh},#1] {};%
\end{scope}
\end{tikzpicture}%
}
\begin{document}
\GoldenBox{I}
\GoldenBox{y}
\GoldenBox[thick, dashed]{long text}
\GoldenBox[draw=blue]{longer text}
\GoldenBox[draw=blue, thick, fill=yellow]{$e = mc^2$}
\GoldenBox[draw=black, thick, fill=yellow!10, rounded corners=2pt]
{$\displaystyle \int_{-\dfrac{\pi}{2}}^{\dfrac{\pi}{2}} dx $}
\GoldenBox[draw=black, thick, fill=yellow!10, rounded
corners=2pt]{$\displaystyle
\begin{pmatrix}
1\\2\\3\\4\\5\\6
\end{pmatrix}$}
\end{document}
答案2
调整并不难。你需要宽度的尺寸和高度:
\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{shapes}
\makeatletter
\newdimen\@myBoxHeight%
\newdimen\@myBoxDepth%
\newdimen\@myBoxWidth%
\newdimen\@myBoxW%
\newdimen\@myBoxH%
\newcommand{\GoldenBox}[2][]{%
\settoheight{\@myBoxHeight}{#2}% Record height of box
\settodepth{\@myBoxDepth}{#2}% Record depth of box
\settowidth{\@myBoxWidth}{#2}% Record width of box
\pgfmathsetlength{\@myBoxW}{max(\@myBoxWidth,(\@myBoxHeight+\@myBoxDepth)*1.61803398875)}%
\pgfmathsetlength{\@myBoxH}{\@myBoxW/1.61803398875}
\tikz \node [shape=rectangle, shape aspect=1,draw=red,inner
sep=2\pgflinewidth, minimum width=\@myBoxW,minimum height=\@myBoxH,#1] {#2};%
}%
\makeatother
\begin{document}
\GoldenBox{I}
\GoldenBox{y}
\GoldenBox[thick, dashed]{long text}
\GoldenBox[draw=blue]{longer rambling text}
\GoldenBox[draw=blue, thick, fill=yellow]{$e = mc^2$}
\GoldenBox[draw=black, thick, fill=yellow!10, rounded
corners=2pt]{$\displaystyle
\begin{pmatrix}
1\\2\\3\\4\\5\\6
\end{pmatrix}$}
\end{document}