3

I am trying to add matrices to an existing document, here is the Github page of the original document and here is the rendered version. However, when I add matrices on every other row the left bracket of the matrix is not rendered. This is the result:

incorrectly renderend matrix

I have tried displaying the brackets in multiple ways however the result always stays the same. This is the current code which I have used:

Transpose \newline 
    \( \begin{bmatrix}
        a & b \\
        c & d \\
        e & f
    \end{bmatrix}^T \) & 
    \smallskip
    \( \begin{bmatrix}
        a & c & e \\
        b & d & f
    \end{bmatrix} \) \\

    Adjoint \newline    
    \( \begin{bmatrix}
        a & b \\
        c & d \\
        e & f
    \end{bmatrix}^\dagger \) & 
    \smallskip
    \( \begin{bmatrix}
        \overline{a} & \overline{c} & \overline{e} \\
        \overline{b} & \overline{d} & \overline{f}
    \end{bmatrix} \) \\

I do not see why this is not rendered properly, especially because it does not happen every row.

Thank you in advance for any help.

EDIT

Completely working document

% Copyright (c) Microsoft Corporation. All rights reserved.
% Licensed under the MIT License.

%!TEX program=xelatex
% This cheatsheet is based on the template
% provided at https://gist.github.com/alexander-yakushev/c773543bf9a957749f79.
\documentclass[10pt,english,landscape]{article}
\usepackage{multicol}
\usepackage{calc}
\usepackage[landscape]{geometry}
\usepackage{color,graphicx,overpic}

% https://tex.stackexchange.com/a/192067
\usepackage{fontawesome}
  \newfontfamily{\FA}{[FontAwesome.otf]}

% Known issue in {fontspec}: https://github.com/wspr/fontspec/issues/312#issuecomment-342125206
% Since {fontspec} is included only when compiling with XeTeX, we guard
% our fix accordingly.
\usepackage{ifxetex}
    \ifxetex
        \let\latinencoding\relax
        \usepackage{fontspec}
        \setmainfont{Segoe UI}
        \setmonofont{Consolas}
    \fi

% \usepackage[T1]{fontenc}
% \usepackage[bitstream-charter]{mathdesign}
\usepackage[utf8]{inputenc}
\usepackage{url}
\usepackage{amsfonts}
\usepackage{array,booktabs}
\usepackage{textcomp}
\usepackage[usenames,dvipsnames,table]{xcolor}
\usepackage[most]{tcolorbox}
\usepackage{menukeys}
\usepackage{tabularx}
\usepackage{multirow}
\usepackage{colortbl}
\usepackage{tikz}
\usepackage{environ}
\usepackage{braket}

\usetikzlibrary{calc}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\geometry{top=1cm,left=1cm,right=1cm,bottom=1cm}

\pagestyle{empty} % Turn off header and footer

% \renewcommand\rmdefault{phv} % Arial
% \renewcommand\sfdefault{phv} % Arial

% Redefine section commands to use less space
\makeatletter
\renewcommand{\section}{\@startsection{section}{1}{0mm}%
  {-1ex plus -.5ex minus -.2ex}%
  {0.5ex plus .2ex}%x
  {\normalfont\large\bfseries}}
\renewcommand{\subsection}{\@startsection{subsection}{2}{0mm}%
  {-1explus -.5ex minus -.2ex}%
  {0.5ex plus .2ex}%
  {\normalfont\normalsize\bfseries}}
\renewcommand{\subsubsection}{\@startsection{subsubsection}{3}{0mm}%
  {-1ex plus -.5ex minus -.2ex}%
  {1ex plus .2ex}%
  {\normalfont\small\bfseries}}
\makeatother

\setcounter{secnumdepth}{0} % Don't print section numbers
\setlength{\parindent}{0pt}
\setlength{\parskip}{0pt plus 0.5ex}

\definecolor{TableHead}{rgb}{0.353, 0.329, 0.667}
\definecolor{TableRow}{rgb}{0.816, 0.812, 0.902}

\NewEnviron{keysref}[1]{
  % \begin{center}
  \smallskip
  \begin{tikzpicture}
      \rowcolors{1}{}{TableRow}

      \node (tbl) [inner sep=0pt] {
        \begin{tabular}{p{2.5cm}p{5.05cm}}
          \rowcolor{TableHead}
          \multicolumn{2}{l}{\normalsize\textbf{\color{white}{#1}}}\parbox{0pt}{\rule{0pt}{0.3ex+\baselineskip}}\\
          \BODY
          \arrayrulecolor{TableHead}\specialrule{.17em}{0em}{.2em}
        \end{tabular}};
      \begin{pgfonlayer}{background}
        \draw[rounded corners=2pt,top color=TableHead,bottom color=TableHead, draw=white]
        ($(tbl.north west)-(0,-0.05)$) rectangle ($(tbl.north east)-(0.0,0.15)$);
        \draw[rounded corners=2pt,top color=TableHead,bottom color=TableHead, draw=white]
        ($(tbl.south west)-(0.0,-0.11)$) rectangle ($(tbl.south east)-(-0.0,-0.02)$);
      \end{pgfonlayer}
    \end{tikzpicture}
  % \end{center}
}

% https://tex.stackexchange.com/a/102523
\newcommand{\forceindent}[1]{\leavevmode{\parindent=#1\indent}}

%% CUSTOM NOTATION %%

\newcommand{\qs}{Q\#}
\newcommand{\unixlike}{\hfill\faApple\faLinux}
\newcommand{\ctrllike}{\hfill\faWindows\faLinux}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\raggedright\

\Large{\qs~0.10 Language Quick Reference}

\footnotesize
\begin{multicols}{3}



  \begin{keysref}{Linear Algebra}
    Transpose \newline 
    \( \begin{bmatrix}
        a & b \\
        c & d \\
        e & f
    \end{bmatrix}^T \) & 
    \smallskip
    \( \begin{bmatrix}
        a & c & e \\
        b & d & f
    \end{bmatrix} \) \\

    Adjoint \newline    
    \( \begin{bmatrix}
        a & b \\
        c & d \\
        e & f
    \end{bmatrix}^\dagger \) & 
    \smallskip
    \( \begin{bmatrix}
        \overline{a} & \overline{c} & \overline{e} \\
        \overline{b} & \overline{d} & \overline{f}
    \end{bmatrix} \) \\
  \end{keysref}

\end{multicols}

\end{document} 
vincent
  • 33

1 Answers1

4

I think this is some kind of clash between whatever wizardry xcolor does for the \rowcolors macro, and the double typesetting that amsmath uses (first pass measures and second generates the output if I remember correctly). Here is a minimal example that illustrates the problem:

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{amsmath}
\begin{document}
\rowcolors{1}{}{cyan}
\begin{tabular}{c}
AAA\\
\( \begin{bmatrix}                                                                                                                                            
a & b \\                                                                                                                                                      
c & d                                                                                                                                                         
\end{bmatrix} \)                                                                                                                                              
\end{tabular}
\end{document}

bad matrix

And here is a hack to get around it:

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{amsmath}
\newbox\matrixbox
\sbox\matrixbox{\( \begin{bmatrix}
a & b \\
c & d
\end{bmatrix} \)}
\begin{document}
\rowcolors{1}{}{cyan}
\begin{tabular}{c}
AAA\\
\usebox\matrixbox
\end{tabular}
\end{document}

good matrix

The point here is that the problem goes away if the matrix is typeset before attempting to create the table. I'm 99% certain a better method exists, but at the moment I don't know what it is.

Ian Thompson
  • 43,767
  • the setbox is probably as good as any, actually it's not due to double typesetting (bmatrix is a very thin wrapper around array and doesn't use the system used by align(ed)) It's simply that the colortbl redefinitions to add the coloured panels have a habit of applying to nested tables – David Carlisle Dec 17 '19 at 23:25
  • @Ian Thompson, thank you for your help. I have an updated file which is on github: https://raw.githubusercontent.com/microsoft/QuantumKatas/1499001ba3bfc7c6ec97aa229b24bdefa7e62351/quickref/qsharp-quick-reference.tex. When running this I keep getting Undefined control sequence for the boxes, the results still looks good. However, I am not sure if the error could be ignored, do you have any insights on this? – vincent Jan 06 '20 at 15:41
  • 2
    @vincent --- The easy error: you are missing some dollars on line 93 (going into a rotatebox takes you out of math mode). The harder error: I don't really know why you get the Undefined control sequence message, but it goes away if you load amsmath and graphicx first, then do all your sboxes and finally load all your other packages and do everything else. Don't ask why though; there is something going on inside the packages which is beyond my TeX knowledge! – Ian Thompson Jan 06 '20 at 21:21
  • thank you Ian and especially @vincent – tbrodbeck Sep 14 '21 at 15:32
  • I have the same clash with \rowcolors{2}{white}{white} in my document and a {bmatrix} environment in my appendix (not in a table). Only the left bracket wouldn't render in the pdf. @IanThompson 's comments helped as a work around, but I had more than 5 matrices I would have not bothered with row coloring. – MilleCodex Nov 26 '23 at 01:22