Graphics, Figures & TablesTightly fitting a frame around table with TikZ

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
karlisrepsons
Posts: 50
Joined: Sat May 23, 2009 10:13 pm

Tightly fitting a frame around table with TikZ

Post by karlisrepsons »

The following example illustrates a couple of problems, the worst of which is the fact that TikZ is drawing the subsequent layer on the background layer with a space in between... Any idea how to control the distance between red and green lines?
make red closer to green and center..?
make red closer to green and center..?
TikZnonFit.jpeg (10.21 KiB) Viewed 4205 times

Code: Select all

\documentclass{minimal}

\usepackage[papersize={2.5cm, 2.5cm}]{geometry}
\usepackage{fontspec}
\usepackage{xcolor}
\usepackage{tabu}
\usepackage{tikz}

\geometry{
   showframe,
   inner=4mm,
   outer=4mm,
   top=4mm,
   bottom=4mm
}
\usetikzlibrary{
   backgrounds,
   calc
}

\tabulinesep=0pt
\tabcolsep=0pt
\tabulinestyle{1pt}

\newcommand{\textmode} {%
   \fontsize{24}{0}%
   \selectfont%
}

\begin{document}
\vspace*{\fill}

\begin{center}
   \begin{tabu} spread 0pt
   {|X[-1, c, m]|}
      \tabucline-
      \begin{tikzpicture}
         \node (tbl) {%
            \taburulecolor{green}%
            \begin{tabu} spread 0pt
            {|X[-1, c, m]|}
              \tabucline-
               \textmode%
               A
             \\
              \tabucline-
            \end{tabu}%
         };
         \begin{pgfonlayer}{background}
            \draw[line width=1pt, draw=red]
                 ($(tbl.north east) - (0,0)$)
                 rectangle ($(tbl.south west) + (0,0)$);
         \end{pgfonlayer}
      \end{tikzpicture}
     \\
      \tabucline-
   \end{tabu}
\end{center}

\vspace*{\fill}
\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

karlisrepsons
Posts: 50
Joined: Sat May 23, 2009 10:13 pm

Tightly fitting a frame around table with TikZ

Post by karlisrepsons »

Got help in comp.text.tex; here I post a partial solution:

Code: Select all

\documentclass{article}

\usepackage[papersize={2.5cm, 2.5cm}]{geometry}
\usepackage{fontspec}
\usepackage{xcolor}
\usepackage{tabu}
\usepackage{tikz}

\geometry{
   showframe,
   inner=4mm,
   outer=4mm,
   top=4mm,
   bottom=4mm
}
\usetikzlibrary{
   backgrounds,
   calc
}

\tabulinesep=0pt
\tabcolsep=0pt
\tabulinestyle{1pt}

\newcommand{\textmode} {%
   \fontsize{24}{0}%
   \selectfont%
}

\begin{document}
\vspace*{\fill}
\noindent%
\begin{center}
   \taburulecolor{yellow}%
   \begin{tabu} spread 0pt
   {|X[-1, c, m]|}
      \tabucline-
      \begin{tikzpicture}[inner sep = 0pt, outer sep = 0pt]
         \node (tbl) {%
            \taburulecolor{green}%
            \begin{tabu} spread 0pt
            {|X[-1, c, m]|}
              \tabucline-
               \textmode%
               A
             \\
              \tabucline-
            \end{tabu}%
         };
         \begin{pgfonlayer}{background}
            \draw[line width=4pt, draw=red]
                 ($(tbl.north east) - (0,0)$)
                 rectangle ($(tbl.south west) + (0,0)$);
         \end{pgfonlayer}
      \end{tikzpicture}
     \\
      \tabucline-
   \end{tabu}
\end{center}
\vspace*{\fill}
\end{document}
Anyone can explain why the two

Code: Select all

\vspace*{\fill}
lines can't make the result be really in the middle? Note that documentclass got changed for "article", because with "minimal" it didn't work...
karlisrepsons
Posts: 50
Joined: Sat May 23, 2009 10:13 pm

Tightly fitting a frame around table with TikZ

Post by karlisrepsons »

Here I'd like to share another example where I solved the original problem with excessive spacing. It looks like this:
Demonstration of tabu and TikZ frame spacing control
Demonstration of tabu and TikZ frame spacing control
MultiframesWithTikZandTabu.jpeg (21.22 KiB) Viewed 4169 times
And the code is as follows:

Code: Select all

\documentclass{article}

\usepackage{calc}
\usepackage[papersize={7cm, 7cm}]{geometry}
\usepackage{fontspec}
\usepackage{xcolor}
\usepackage{tabu}
\usepackage{tikz}

\geometry{
   showframe,
   inner=4mm,
   outer=4mm,
   top=4mm,
   bottom=4mm
}
\usetikzlibrary{
   backgrounds,
   calc
}

\definecolor{myblue}{rgb}{0, 0.4, 0.7}
\definecolor{mygreen}{rgb}{0, 1, 0}
\definecolor{myred}{rgb}{1, 0, 0}
\definecolor{myorange}{rgb}{1, 0.7, 0}
\definecolor{mypink}{rgb}{1, 0, 1}

\newlength{%
\thicknessLine}
   \setlength{\thicknessLine}{3pt}
\newlength{%
\testSpaceInterlines}
   \setlength{\testSpaceInterlines}{1pt}
\newlength{%
\frameEscapeHalf}
   \setlength{\frameEscapeHalf}{\testSpaceInterlines + \thicknessLine/2}

\tabulinesep=\testSpaceInterlines
\tabcolsep=\testSpaceInterlines
\tabulinestyle{\thicknessLine}

\defaultfontfeatures{Mapping=tex-text}
\setmainfont{Times New Roman}
\newcommand{\textmode} {%
   \color{myblue}%
   \fontsize{24}{0}%
   \bfseries%
   %\itshape
}

\newlength{\auxWidth}
\newlength{\auxHeight}
\newlength{\auxDepth}

\newcommand{\bAnalyze}[1]{%
   \settowidth{\auxWidth} {\usebox{#1}}%
   \settoheight{\auxHeight} {\usebox{#1}}%
   \settodepth{\auxDepth} {\usebox{#1}}%
   \color{mypink}%
   \usebox{#1}%
   \llap{%
      \rule[-\thicknessLine]{\auxWidth}{\thicknessLine}%
   }%
   \color{mygreen}%
   \vrule width \thicknessLine height \auxHeight depth 0pt \relax%
   \color{myred}%
   \llap{%
      \vrule width \thicknessLine height 0pt depth \auxDepth \relax%
   }%
}

\newsavebox{\tableBox}

\begin{document}
   \thispagestyle{empty}
   \savebox{\tableBox}{%
      \taburulecolor{myblue}%
      \begin{tabu} spread 0pt
      {|X[-1, c, m]|}
         \tabucline-
         {%
            \taburulecolor{myorange}
            \begin{tabu} spread 0pt {|X[-1, c, m]|}
               \tabucline-
               \textmode%
               CONTENTS \\
               \tabucline-
            \end{tabu}%
         }
       \\
         \tabucline-
      \end{tabu}%
   }
   \vspace*{\fill}
   \begin{center}
      \taburulecolor{mygreen}%
      \begin{tabu} spread 0pt
      {|X[-1, c, m]|}
         \tabucline-
         \begin{tikzpicture}[inner sep = 0pt, outer sep = 0pt]
            \node (tbl) {%
               \bAnalyze{\tableBox}%
            };
            \begin{pgfonlayer}{background}
               \draw[line width=\thicknessLine,draw=mypink]
                    ($(tbl.north east) + (\frameEscapeHalf, \frameEscapeHalf)$)
                    rectangle ($(tbl.south west) - (\frameEscapeHalf, \frameEscapeHalf)$);
            \end{pgfonlayer}
         \end{tikzpicture}
       \\
         \tabucline-
      \end{tabu}
   \end{center}
   \vspace*{\fill}
\end{document}
Post Reply