17

I would like to create special frame around my equations. This frame should obey to the golden ratio rule. This means that its width divided by its height should give 1.61803398875.

If possible, the code will adapt to choose the long side. For example for a long column vector, the height will be 1.61803398875 times longer than its width.

I've found the following code in another question in this forum (here). It uses tikz but I can't figure out how to modify it to obtain a golden ratio frame around my equations...

\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}

Thanks for your help in advance.

13

Horizontal GoldenBox

Here is a (pure TikZ!) solution via the fit, calc and backgrounds TikZ libraries where all equations are horizontally aligned:

enter image description here

\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}

Vertical or Horizontal GoldenBox

enter image description here

\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}
  • Thanks a lot! In fact I was having this issue in a document just some minutes ago... Will it be possible to make this box choose the long side and flip the box according to it (for example for a long column vector...)? – jrojasqu Mar 10 '13 at 12:49
  • @jrojasqu I edited my anwser. – Paul Gaborit Mar 10 '13 at 13:25
15

The adaptation is not so hard. You need dimensions for the width and height:

Sample output

\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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.