3

I'm having problems (again) with vertical alignements in tables, but apparently, the code is problematic when compiled on my computer, but not when compiled on overleaf. For instance:

Overleaf rendition:

overleaf rendition

TeXStudio rendition on my PC:

TeXStudio rendition on my PC

And here is the code:

\documentclass[a4paper, 10pt]{article}
\usepackage{bm}
\usepackage[dvipsnames,table]{xcolor} 
\usepackage{cmbright}
\renewcommand{\sfdefault}{cmbr}
\everymath{\displaystyle}

\begin{document}


\begin{tabular}{|
                >{\centering\arraybackslash}m{10ex}| 
                >{\centering\arraybackslash}m{4ex}| 
                >{\centering\arraybackslash}m{4ex}|
                >{\centering\arraybackslash}m{4ex}|
                >{\centering\arraybackslash}m{4ex}|
                >{\centering\arraybackslash}m{4ex}|
                @{}m{0pt}@{}}
                    \hline
                    \cellcolor{gray!25}{\textbf{Degrees}}
                    & \cellcolor{green!15}{$0^{\circ}$}
                    & \cellcolor{green!15}{$30^{\circ}$}
                    & \cellcolor{green!15}{$45^{\circ}$}
                    & \cellcolor{green!15}{$60^{\circ}$}
                    & \cellcolor{green!15}{$90^{\circ}$}&\\[4ex]
                    \hline
                    \cellcolor{gray!25}{\textbf{Radians}}
                    & \cellcolor{green!15}{$ 0$}
                    & \cellcolor{green!15}{ $ \frac{\pi}{6}$}
                    & \cellcolor{green!15}{$ \frac{\pi}{4}$}
                    & \cellcolor{green!15}{$ \frac{\pi}{3}$}
                    & \cellcolor{green!15}{$ \frac{\pi}{2}$}&\\[4ex]
                    \hline
                    \cellcolor{blue!15}{\textbf{sin}($\bm{\theta}$)}
                    & $ 0$
                    & $ \frac{1}{2}$
                    & $ \frac{\sqrt 2}{2}$
                    & $ \frac{\sqrt 3}{2}$
                    & $ 1$&\\[4ex]
                    \hline
                    \cellcolor{blue!15}{\textbf{cos}($\bm{\theta}$)}
                    & $ 1$
                    & $ \frac{\sqrt 3}{2}$
                    & $ \frac{\sqrt 2}{2}$
                    & $ \frac{1}{2}$
                    & $ 0$&\\[4ex]
                    \hline
                    \cellcolor{blue!15}{\textbf{tan}($\bm{\theta}$)}
                    & $ 0$
                    & $ \frac{\sqrt 3}{3}$
                    & $ 1$
                    & $ \sqrt 3$
                    &\cellcolor{black}{}\\[4ex]
                    \hline
        \end{tabular}


\end{document}

I'm using :

  • the latest TeXStudio release (TeXstudio 2.12.14 (git 2.12.14) Using Qt Version 5.8.0)

  • on windows 7

  • pdflatex as default compiler

  • latex was implemented on my computer with MiKTeX.

Does anyone know why this is happening?

Thanks

2
  • 6
    you are using a newer version of the array package with a fixed m column implementation. Apr 30 '19 at 20:26
  • 3
    Note that currently Overleaf uses TeX Live 2016, while TeX Live 2019 was released yesterday. A staff member at Overleaf said they're planning to install TeX Live 2018 in the “near future”. If you must use it, you have to make do with what they give you.
    – egreg
    Apr 30 '19 at 21:12
1

Actually, you code only needs two modification to compile correctly. If you look at page 6 in the documentation for the package array, the author describes the following change:

Prior to version 2.4f the space added by the optional argument to \ was added inside an m-cell if the last column was of type m. As a result that cell was vertically centered with that space inside, resulting in a strange offset. Since 2.4f, this space is now added after centering the cell.

At the end of all your rows, you have set a spacing of 4ex (\\[4ex]). I assume this is to ensure equal height of all row. However, the simplest solution I found to secure equal height of the rows, is to put a zero width rule of require height into all rows (\rule{0pt}{4ex}). I my MWE, I have added +1\tabcolsep to the height to compensate for the vertical lines in the table (and added the package calc do the maths). This is of cause not absolute necessary. I redefined the last column in your MWE to:

@{}>{\rule{0pt}{4ex+1\tabcolsep}}m{0pt}@{}}

removed [4ex] five places, and loaded calc in the second line

usepackage{bm, calc}

Unfortunately, you have an error in you MWE, which you have to correct to have the last row correctly spaced. You had forgotten to add the last ampersand & in the last row of the table.

&\cellcolor{black}{}
&\\                 % Last `&` cell was missing

With these three modifications, I was able to compile your code both on my updated local system and on Overleaf, with similar result. A complete MWE:

\documentclass[a4paper, 10pt]{article}
\usepackage{bm, calc}                      % Added the package `calc`
\usepackage[dvipsnames,table]{xcolor} 
\usepackage{cmbright}
\renewcommand{\sfdefault}{cmbr}
\everymath{\displaystyle}

\begin{document}


\begin{tabular}{|
                >{\centering\arraybackslash}m{10ex}| 
                >{\centering\arraybackslash}m{4ex}| 
                >{\centering\arraybackslash}m{4ex}|
                >{\centering\arraybackslash}m{4ex}|
                >{\centering\arraybackslash}m{4ex}|
                >{\centering\arraybackslash}m{4ex}|
                @{}>{\rule{0pt}{4ex+1\tabcolsep}}m{0pt}@{}}  % Zero width rule 
                    \hline
                    \cellcolor{gray!25}{\textbf{Degrees}}
                    & \cellcolor{green!15}{$0^{\circ}$}
                    & \cellcolor{green!15}{$30^{\circ}$}
                    & \cellcolor{green!15}{$45^{\circ}$}
                    & \cellcolor{green!15}{$60^{\circ}$}
                    & \cellcolor{green!15}{$90^{\circ}$}&\\
                    \hline
                    \cellcolor{gray!25}{\textbf{Radians}}
                    & \cellcolor{green!15}{$0$}
                    & \cellcolor{green!15}{$\frac{\pi}{6}$}
                    & \cellcolor{green!15}{$\frac{\pi}{4}$}
                    & \cellcolor{green!15}{$\frac{\pi}{3}$}
                    & \cellcolor{green!15}{$\frac{\pi}{2}$}&\\
                    \hline
                    \cellcolor{blue!15}{\textbf{sin}($\bm{\theta}$)}
                    & $0$
                    & $\frac{1}{2}$
                    & $\frac{\sqrt 2}{2}$
                    & $\frac{\sqrt 3}{2}$
                    & $1$&\\
                    \hline
                    \cellcolor{blue!15}{\textbf{cos}($\bm{\theta}$)}
                    & $1$
                    & $\frac{\sqrt 3}{2}$
                    & $\frac{\sqrt 2}{2}$
                    & $\frac{1}{2}$
                    & $0$&\\
                    \hline
                    \cellcolor{blue!15}{\textbf{tan}($\bm{\theta}$)}
                    & $0$
                    & $\frac{\sqrt 3}{3}$
                    & $1$
                    & $\sqrt 3$
                    &\cellcolor{black}{}
                    &\\                         % Last `&` cell was missing
                    \hline
        \end{tabular}

\end{document}

Overleaf result:

Overleaf result

Result from updated MikTEx 2.9:

Miktex result

I removed some spaces after some of the $ so the cells look centred.

As a bonus, I enclose a code optimised MWE:

\documentclass[a4paper, 10pt]{article}
\usepackage{bm, cmbright, calc}
\usepackage[dvipsnames,table]{xcolor} 
\renewcommand{\sfdefault}{cmbr}
%\everymath{\displaystyle}                              % Nicer without displaystyle?

\setlength\extrarowheight{1pt}                          % Move cell content down
\newcolumntype{C}{>{\centering\arraybackslash$}m{4ex}<{$}}
\newcolumntype{F}{>{\bfseries\centering\arraybackslash\columncolor{blue!15}}m{10ex}}
\newcolumntype{L}{@{}>{\rule{0pt}{4ex+1\tabcolsep}}m{0pt}@{}}   % To ensure equal row height 

 % Shortcuts
\newcommand*{\gray}{\cellcolor{gray!25}}
\newcommand*{\black}{\cellcolor{black}}
\newcommand*{\green}{\cellcolor{green!15}}

\arrayrulecolor{gray!15}                        % Light gray table rules

\begin{document}

\begin{tabular}{|F|*{5}{C|}L}                                                    

                \firsthline%
                \gray{}Degrees        % Gray cell over blue column, \rowcolor not possible to use
                &   \green\kern0.4em0^{\circ}
                &   \green30^{\circ}
                &   \green45^{\circ}
                &   \green60^{\circ}
                &   \green90^{\circ}
                &   \\
                \hline%
                \gray{}Radians
                &   \green 0
                &   \green\frac{\pi}{6}
                &   \green\frac{\pi}{4}
                &   \green\frac{\pi}{3}
                &   \green\frac{\pi}{2}
                &\\
                \hline%
                sin($\bm{\theta}$)
                &   0
                &   \frac{1}{2}
                &   \frac{\sqrt 2}{2}
                &   \frac{\sqrt 3}{2}
                &   1
                &\\
                \hline%
                cos($\bm{\theta}$)
                &   1
                &   \frac{\sqrt 3}{2}
                &   \frac{\sqrt 2}{2}
                &   \frac{1}{2}
                &   0
                &\\
                \hline%
                tan($\bm{\theta}$)
                &   0
                &   \frac{\sqrt 3}{3}
                &   1
                &   \sqrt 3
                &   \black
                &   \\
                \hline%
    \end{tabular}
\end{document}

And a cals-version:

\documentclass[a4paper, 10pt]{article}
\usepackage{bm, cmbright, calc, cals}
\usepackage[dvipsnames,table]{xcolor} 
\renewcommand{\sfdefault}{cmbr}
\everymath{\displaystyle}                                           % Nicer without displaystyle?

\begin{document}

\begin{calstable}
\colwidths{{56pt}{33pt}{33pt}{33pt}{33pt}{33pt}} % 6 colums
\makeatletter
\def\cals@framers@width{0.8pt}
\def\cals@framecs@width{0.8pt}
\cals@setpadding{Ag}
\cals@setcellprevdepth{Al}
\def\cals@cs@width{0.4pt}
\def\cals@rs@width{0.4pt}

% R1
\brow
    \def\cals@bgcolor{gray!15}
    \alignC\cell{\vfil\textbf{Degrees}}
    \def\cals@bgcolor{green!15}
    \alignC\cell{\vfil $\kern0.4em0^{\circ}$}
    \alignC\cell{\vfil $30^{\circ}$}
    \alignC\cell{\vfil $45^{\circ}$}
    \alignC\cell{\vfil $60^{\circ}$}
    \alignC\cell{\vfil $90^{\circ}$}
    \ht\cals@current@row=33pt
\erow
% R2
\brow
    \def\cals@bgcolor{gray!15}
    \alignC\cell{\vfil \textbf{Radians}}
    \def\cals@bgcolor{green!15}
    \alignC\cell{\vfil $0$}
    \alignC\cell{\vfil $\frac{\pi}{6}$}
    \alignC\cell{\vfil $\frac{\pi}{4}$}
    \alignC\cell{\vfil $\frac{\pi}{3}$}
    \alignC\cell{\vfil $\frac{\pi}{2}$}
    \ht\cals@current@row=33pt
\erow
% R3
\brow
    \def\cals@bgcolor{blue!15}
    \alignC\cell{\vfil \textbf{sin}($\bm{\theta}$)}
    \def\cals@bgcolor{}
    \alignC\cell{\vfil $0$}
    \alignC\cell{\vfil $\frac{1}{2}$}
    \alignC\cell{\vfil $\frac{\sqrt 2}{2}$}
    \alignC\cell{\vfil $\frac{\sqrt 3}{2}$}
    \alignC\cell{\vfil $1$}
    \ht\cals@current@row=33pt
\erow
% R4
\brow
    \def\cals@bgcolor{blue!15}
    \alignC\cell{\vfil \textbf{cos}($\bm{\theta}$)}
    \def\cals@bgcolor{}
    \alignC\cell{\vfil $1$}
    \alignC\cell{\vfil $\frac{\sqrt 3}{2}$}
    \alignC\cell{\vfil $\frac{\sqrt 2}{2}$}
    \alignC\cell{\vfil $\frac{1}{2}$}
    \alignC\cell{\vfil $0$}
    \ht\cals@current@row=33pt
\erow
% R5
\brow
    \def\cals@bgcolor{blue!15}
    \alignC\cell{\vfil \textbf{tan}($\bm{\theta}$)}
    \def\cals@bgcolor{}
    \alignC\cell{\vfil $0$}
    \alignC\cell{\vfil $\frac{\sqrt 3}{3}$}
    \alignC\cell{\vfil $1$}
    \alignC\cell{\vfil $\sqrt 3$}
    \def\cals@bgcolor{black}
    \alignC\cell{}
    \ht\cals@current@row=33pt
\erow\makeatother
\end{calstable}

\end{document}

enter image description here

6
  • Hey, thanks for the input. Though this code doesn't compile on my end.
    – Mogu
    May 2 '19 at 18:04
  • @Chris I assume you are not interested in the updated code, but I manage to have you original code working correctly with only two small modifications.
    – Sveinung
    May 4 '19 at 21:07
  • @ Sveinung no no, I am. Just very busy at the moment, I'll check it out asap. Thanks
    – Mogu
    May 5 '19 at 7:43
  • @Chris Take your time :)
    – Sveinung
    May 5 '19 at 13:38
  • @ Sveinung Alright, so thanks a lot for all the work you've invested. I think I'll end up using the latest version you've added (the one before the cals version). Alhought the previous answers have been useful too, I like using tabular instead of tikz in the case, so I'll move the tick to your post.
    – Mogu
    Jun 10 '19 at 10:59
3

see, if the following solution gives the same result on your PC and on Overleaf:

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix}
\usepackage{cmbright}
\renewcommand{\sfdefault}{cmbr}
\usepackage{bm}
\usepackage{siunitx}
\usepackage{amsmath}

\begin{document}
    \begin{tikzpicture}
\matrix [matrix of math nodes,
         nodes={draw, minimum height=6.4ex, minimum width=2.5em,
                anchor=center},
         column sep=-\pgflinewidth,
         row sep=-\pgflinewidth,
         column 1/.style = {nodes={fill=blue!15, minimum width=5em}},
         row 1/.style = {nodes={preaction={fill=green!15}}},
         row 2/.style = {nodes={preaction={fill=green!15}}}
         ]
{
|[fill=gray!25]| \textbf{Degrees}
    & \SI{ 0}{\degree}      & \SI{30}{\degree}
    & \SI{45}{\degree}      & \SI{60}{\degree}
    & \SI{90}{\degree}                  \\
|[fill=gray!25]| \textbf{Radians}
    & 0                     & \dfrac{\pi}{6}
    & \dfrac{\pi}{4}        & \dfrac{\pi}{3}
    & \dfrac{\pi}{2}                    \\
\textbf{sin}(\bm{\theta})
    & 0                     & \dfrac{1}{2}
    & \dfrac{\sqrt{2}}{2}   & \dfrac{\sqrt{3}}{2}
    & 1                                 \\
\textbf{cos}(\bm{\theta})
    & 1                     & \dfrac{\sqrt{3}}{2}
    & \dfrac{\sqrt{2}}{2}   & \dfrac{1}{2}
    & 0                                 \\
\textbf{tan}(\bm{\theta})
    & 0                     & \dfrac{\sqrt{3}}{3}
    & 1                     & \sqrt{3}
    & |[fill=black]|                    \\
};
\end{tikzpicture}
\end{document}

enter image description here

2
  • Thanks, works fine! I honestly hoped I could manage using the array/tabular environments for all tables, but well, that'll do ;)
    – Mogu
    May 1 '19 at 9:22
  • @Chris, at least for now it is independent on texlive version :-) (i hope)
    – Zarko
    May 1 '19 at 9:27

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.