Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 12 down vote accepted

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}
share|improve this answer
    
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
    
Perfect! Thanks a lot, it works great! –  jrojasqu Mar 10 '13 at 14:03

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}
share|improve this answer
    
Great! Exactly what I needed! Thanks! –  jrojasqu Mar 10 '13 at 11:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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