0

How can I change my table style into a magic square table style?

\begin{table}[htbp]
  \centering
  \caption{Add caption}
    \begin{tabular}{rrr}
    \toprule
    +1      & +5      & -2 \\
    \midrule
            & -3      &  \\
            & +4      &  \\
    \bottomrule
    \end{tabular}%
  \label{tab:addlabel}%
\end{table}%

enter image description here

  • 1
    Hmmm, looking forward to what you mean by "tablet style" and "magic square table style". These sound interesting... – Werner Nov 15 '17 at 23:27
  • 1
    Is this question a duplicate of this: how to change table style? – Werner Nov 15 '17 at 23:27
  • @werner sorry...table style – ryuk Nov 15 '17 at 23:32
  • I know what a magic square is, but what do you mean by "magic square table style" what do you want to change in the above code (it would have been better if the example code had been a complete document to produce the image) – David Carlisle Nov 15 '17 at 23:47
  • I mean that I would like that the horizontal and vertical lines of the table are visible, and also that the dimension of the single cell is a square and not a rectangle. – ryuk Nov 16 '17 at 0:05
3

The following example sets each cell entry in a box that is the width of the baseline skip (that varies depending on the \arraystretch that may have been redefined). This gives a square output.

enter image description here

\documentclass{article}

\usepackage{collcell}
\newcommand{\fwcell}[1]{\makebox[\arraystretch\normalbaselineskip]{$#1$}}

\begin{document}

\begingroup
\setlength{\tabcolsep}{0pt}
\begin{tabular}{ | *{3}{>{\collectcell\fwcell}c<{\endcollectcell} |} }
  \hline
  1 & 2 & 3 \\
  \hline
  4 & 5 & 6 \\
  \hline
  7 & 8 & 9 \\
  \hline
\end{tabular}
\quad
\renewcommand{\arraystretch}{2}%
\begin{tabular}{ | *{3}{>{\collectcell\fwcell}c<{\endcollectcell} |} }
  \hline
  1 & 2 & 3 \\
  \hline
  4 & 5 & 6 \\
  \hline
  7 & 8 & 9 \\
  \hline
\end{tabular}
\endgroup

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