在添加命令 \makegapedcells 后在表格中使用“m”选项时出错

在添加命令 \makegapedcells 后在表格中使用“m”选项时出错

\makegapedcells我从包中添加了命令,现在我的表格在使用字段makecell时出现错误。m

似乎有些冲突。这是 MW。

\documentclass[12pt]{book}

\usepackage{array}
\usepackage{makecell} %make sure before arydshln
%see https://tex.stackexchange.com/questions/563115/how-to-add-more-padding-to-table-rows-when-table-has-array-inside-it
\setcellgapes{5pt} %added march 14,2021
\makegapedcells  %added march 14,2021 
  
\begin{document}  
\begin{tabular}[c]{|m{2in}|m{4in}|}\hline
cell 1
&
cell 2
\end{tabular}
\end{document}

现在,lualatex foo5.tex给出

(base) >lualatex foo5.tex
This is LuaHBTeX, Version 1.12.0 (TeX Live 2020)
 restricted system commands enabled.
(./foo5.tex
LaTeX2e <2020-10-01> patch level 2


 L3 programming layer <2020-12-07> xparse <2020-03-03> (/usr/local/texlive/2020/texmf-dist/tex/latex/base/book.cls
Document Class: book 2020/04/10 v1.4m Standard LaTeX document class
(/usr/local/texlive/2020/texmf-dist/tex/latex/base/bk12.clo)) (/usr/local/texlive/2020/texmf-dist/tex/latex/tools/array.sty) (/usr/local/texlive/2020/texmf-dist/tex/latex/makecell/makecell.sty) (/usr/local/texlive/2020/texmf-dist/tex/latex/l3backend/l3backend-luatex.def) (./foo5.aux) (/usr/local/texlive/2020/texmf-dist/tex/latex/base/ts1cmr.fd)
! Missing $ inserted.
<inserted text>
$
l.11 c
    ell 1
?

如果我将m表中的命令改为p,错误就会消失。此外,如果我删除命令\makegapedcells,错误也会消失。

为什么会发生这种情况?这是一个错误吗?还是我使用方法不正确?

我正在使用它\makegapedcells来获得更多的行间空间,如包中所述makecell

在此处输入图片描述

我知道作为一种解决方法,我可以做以下事情

\documentclass[12pt]{book}

\usepackage{array}
\usepackage{makecell} %make sure before arydshln
\setcellgapes{5pt} %added march 14,2021

\begin{document}  
\begin{tabular}[c]{|m{2in}|m{4in}|}\hline
\makegapedcells
cell 1
&
cell 2
\end{tabular}

\end{document}

现在它可以编译了。但我在许多文件中有 100 个表,我不想像上面那样搜索每个表并进行更改。所以这就是我\makegapedcells在序言中使用的原因。

参考:

https://ctan.math.illinois.edu/macros/latex/contrib/makecell/makecell.pdf

编辑 4/13/2021

下面的答案中发布的补丁不起作用。我仍然收到相同的错误

\documentclass[12pt]{article}

\usepackage{amsmath}
\usepackage{makecell}
\setcellgapes{5pt} 
\makegapedcells  %I need to add this to make cell spacing larger. 

%%FROM https://tex.stackexchange.com/questions/319768/are-m-column-type-and-makecell-incompatible   
\makeatletter
\renewcommand\mcell@classz{\@classx
    \@tempcnta \count@
    \prepnext@tok
    \@addtopreamble{%\mcell@mstyle
        \ifcase\@chnum
        \hfil
        \mcell@agape{\d@llarbegin\insert@column\d@llarend}\hfil \or
        \hskip1sp
        \mcell@agape{\d@llarbegin\insert@column\d@llarend}\hfil \or
        \hfil\hskip1sp
        \mcell@agape{\d@llarbegin \insert@column\d@llarend}\or
        \mcell@agape{$\vcenter
            \@startpbox{\@nextchar}\insert@column\@endpbox$}\or
        \mcell@agape{\vtop
            \@startpbox{\@nextchar}\insert@column\@endpbox}\or
        \mcell@agape{\vbox
            \@startpbox{\@nextchar}\insert@column\@endpbox}%
        \fi
        \global\let\mcell@left\relax\global\let\mcell@right\relax
    }\prepnext@tok}

\makeatletter

 
\begin{document}  

\begin{tabular}[c]{|m{2in}|m{4in}|}\hline
cell 1
&
cell 2
\end{tabular}

\end{document}

给出

lualatex foo5.tex
This is LuaHBTeX, Version 1.12.0 (TeX Live 2020)
 restricted system commands enabled.
(./foo5.tex
LaTeX2e <2020-10-01> patch level 2


 L3 programming layer <2020-12-07> xparse <2020-03-03> (/usr/local/texlive/2020/texmf-dist/tex/latex/base/article.cls
Document Class: article 2020/04/10 v1.4m Standard LaTeX document class
(/usr/local/texlive/2020/texmf-dist/tex/latex/base/size12.clo)) (/usr/local/texlive/2020/texmf-dist/tex/latex/amsmath/amsmath.sty
For additional information on amsmath, use the `?' option.
(/usr/local/texlive/2020/texmf-dist/tex/latex/amsmath/amstext.sty (/usr/local/texlive/2020/texmf-dist/tex/latex/amsmath/amsgen.sty)) (/usr/local/texlive/2020/texmf-dist/tex/latex/amsmath/amsbsy.sty) (/usr/local/texlive/2020/texmf-dist/tex/latex/amsmath/amsopn.sty)) (/usr/local/texlive/2020/texmf-dist/tex/latex/makecell/makecell.sty (/usr/local/texlive/2020/texmf-dist/tex/latex/tools/array.sty)) (/usr/local/texlive/2020/texmf-dist/tex/latex/l3backend/l3backend-luatex.def) (./foo5.aux) (/usr/local/texlive/2020/texmf-dist/tex/latex/base/ts1cmr.fd)
! Missing $ inserted.
<inserted text>
$
l.37 c
    ell 1
?

另外,arraystretch实际上不起作用。我需要在单元格中添加额外的空间。下面是一个显示它不起作用的示例

\documentclass[12pt]{article}

\usepackage{amsmath}
\renewcommand{\arraystretch}{3}% does not work
 
\begin{document}  

\begin{center}%
\begin{tabular}[c]{|l|l|l|}\hline
Spin matrix & Eigenvalues & Eigenvectors\\\hline
$S_{x}=\frac{\hbar}{2}%
\begin{bmatrix}
0 & 1\\
1 & 0
\end{bmatrix}
$ & $\frac{\hbar}{2},-\frac{\hbar}{2}$ & $\frac{1}{\sqrt{2}}%
\begin{bmatrix}
1\\
1
\end{bmatrix}
\qquad\frac{1}{\sqrt{2}}%
\begin{bmatrix}
-1\\
1
\end{bmatrix}
$\\\hline
\end{tabular}
\end{center}%

\end{document}

在此处输入图片描述

与使用时进行比较usepackage{makecell}

\documentclass[12pt]{article}

\usepackage{amsmath}
\usepackage{makecell}
\setcellgapes{5pt} 
\makegapedcells  %works as long as m is not used
 
\begin{document}  

\begin{center}%
\begin{tabular}[c]{|l|l|l|}\hline
Spin matrix & Eigenvalues & Eigenvectors\\\hline
$S_{x}=\frac{\hbar}{2}%
\begin{bmatrix}
0 & 1\\
1 & 0
\end{bmatrix}
$ & $\frac{\hbar}{2},-\frac{\hbar}{2}$ & $\frac{1}{\sqrt{2}}%
\begin{bmatrix}
1\\
1
\end{bmatrix}
\qquad\frac{1}{\sqrt{2}}%
\begin{bmatrix}
-1\\
1
\end{bmatrix}
$\\\hline
\end{tabular}
\end{center}%

\end{document}

在此处输入图片描述

以上就是我想要的。但我希望它能与选项一起使用m。所以恐怕下面的答案不能解决这个问题。使用m选项时,补丁仍然会出错。

编辑 4/13/2021

经过更多实验,\makegapedcells可以在前言中使用,但必须在补丁之后使用。如下所示

\documentclass[12pt]{article}

\usepackage{amsmath}
\usepackage{makecell}
\setcellgapes{5pt} 
%%FROM https://tex.stackexchange.com/questions/319768/are-m-column-type-and-makecell-incompatible   
\makeatletter
\renewcommand\mcell@classz{\@classx
    \@tempcnta \count@
    \prepnext@tok
    \@addtopreamble{%\mcell@mstyle
        \ifcase\@chnum
        \hfil
        \mcell@agape{\d@llarbegin\insert@column\d@llarend}\hfil \or
        \hskip1sp
        \mcell@agape{\d@llarbegin\insert@column\d@llarend}\hfil \or
        \hfil\hskip1sp
        \mcell@agape{\d@llarbegin \insert@column\d@llarend}\or
        \mcell@agape{$\vcenter
            \@startpbox{\@nextchar}\insert@column\@endpbox$}\or
        \mcell@agape{\vtop
            \@startpbox{\@nextchar}\insert@column\@endpbox}\or
        \mcell@agape{\vbox
            \@startpbox{\@nextchar}\insert@column\@endpbox}%
        \fi
        \global\let\mcell@left\relax\global\let\mcell@right\relax
    }\prepnext@tok}

\makeatletter

\makegapedcells % HAS to be AFTER the batch, else compiler error

\begin{document}  

\begin{tabular}[c]{|m{2in}|m{4in}|}\hline
    cell 1  &       cell 2\\    \hline
    cell 3  &       cell 4\\    \hline      
    cell 5  &       cell 6\\    \hline
\end{tabular}

\end{document}

现在编译成功

lualatex foo5.tex
This is LuaHBTeX, Version 1.12.0 (TeX Live 2020)
 restricted system commands enabled.
(./foo5.tex
LaTeX2e <2020-10-01> patch level 2
    
 L3 programming layer <2020-12-07> xparse <2020-03-03> (/usr/local/texlive/2020/texmf-dist/tex/latex/base/article.cls
Document Class: article 2020/04/10 v1.4m Standard LaTeX document class
(/usr/local/texlive/2020/texmf-dist/tex/latex/base/size12.clo)) (/usr/local/texlive/2020/texmf-dist/tex/latex/amsmath/amsmath.sty
For additional information on amsmath, use the `?' option.
(/usr/local/texlive/2020/texmf-dist/tex/latex/amsmath/amstext.sty (/usr/local/texlive/2020/texmf-dist/tex/latex/amsmath/amsgen.sty)) (/usr/local/texlive/2020/texmf-dist/tex/latex/amsmath/amsbsy.sty) (/usr/local/texlive/2020/texmf-dist/tex/latex/amsmath/amsopn.sty)) (/usr/local/texlive/2020/texmf-dist/tex/latex/makecell/makecell.sty (/usr/local/texlive/2020/texmf-dist/tex/latex/tools/array.sty)) (/usr/local/texlive/2020/texmf-dist/tex/latex/l3backend/l3backend-luatex.def) (./foo5.aux) (/usr/local/texlive/2020/texmf-dist/tex/latex/base/ts1cmr.fd)
Overfull \hbox (86.81996pt too wide) in paragraph at lines 35--40
[][]
[1{/usr/local/texlive/2020/texmf-var/fonts/map/pdftex/updmap/pdftex.map}] (./foo5.aux))
(see the transcript file for additional information)
 484 words of node memory still in use:
   6 hlist, 2 vlist, 2 rule, 2 glue, 4 kern, 1 glyph, 6 attribute, 52 glue_spec, 6 attribute_list, 1 write nodes
   avail lists: 2:91,3:28,4:1,5:21,6:14,7:210,8:1,9:164
</usr/local/texlive/2020/texmf-dist/fonts/opentype/public/lm/lmroman12-regular.otf>
Output written on foo5.pdf (1 page, 3677 bytes).
Transcript written on foo5.log.

现在我可以使用m类型,也可以\makegapedcells在序言中使用。我只想在序言中使用它,因为其他原因。

答案1

https://tex.stackexchange.com/a/319775/161015进行补丁并将其放入\makegapedcells环境内部而不是序言中。

尝试代码:

\documentclass[12pt]{book}
    
\usepackage{array}  
\usepackage{makecell} %make sure before arydshln    

%%FROM https://tex.stackexchange.com/questions/319768/are-m-column-type-and-makecell-incompatible   
\makeatletter
\renewcommand\mcell@classz{\@classx
    \@tempcnta \count@
    \prepnext@tok
    \@addtopreamble{%\mcell@mstyle
        \ifcase\@chnum
        \hfil
        \mcell@agape{\d@llarbegin\insert@column\d@llarend}\hfil \or
        \hskip1sp
        \mcell@agape{\d@llarbegin\insert@column\d@llarend}\hfil \or
        \hfil\hskip1sp
        \mcell@agape{\d@llarbegin \insert@column\d@llarend}\or
        \mcell@agape{$\vcenter
            \@startpbox{\@nextchar}\insert@column\@endpbox$}\or
        \mcell@agape{\vtop
            \@startpbox{\@nextchar}\insert@column\@endpbox}\or
        \mcell@agape{\vbox
            \@startpbox{\@nextchar}\insert@column\@endpbox}%
        \fi
        \global\let\mcell@left\relax\global\let\mcell@right\relax
    }\prepnext@tok}

\makeatletter
%% *************************************************************************
\begin{document}  
\setcellgapes{5pt} %added march 14,2021
\makegapedcells % put it here <<<<<<<<<<<<<<<<<<<<<<

\begin{tabular}[c]{|m{2in}|m{4in}|}\hline
    cell 1  &       cell 2\\    \hline
    cell 3  &       cell 4\\    \hline      
    cell 5  &       cell 6\\    \hline
\end{tabular}

\end{document}

b

答案2

我建议使用与类型列兼容的nicematrix键。cell-space-limitsm

\documentclass[12pt]{article}

\usepackage{nicematrix}
\NiceMatrixOptions{cell-space-limits=5pt}

\begin{document}  

\begin{center}%
\begin{NiceTabular}[c]{lll}[hvlines]
Spin matrix & Eigenvalues & Eigenvectors\\
$S_{x}=\frac{\hbar}{2}%
\begin{bmatrix}
0 & 1\\
1 & 0
\end{bmatrix}
$ & $\frac{\hbar}{2},-\frac{\hbar}{2}$ & $\frac{1}{\sqrt{2}}%
\begin{bmatrix}
1\\
1
\end{bmatrix}
\qquad\frac{1}{\sqrt{2}}%
\begin{bmatrix}
-1\\
1
\end{bmatrix}
$\\
\end{NiceTabular}
\end{center}


\begin{center}
\begin{NiceTabular}[c]{>{\raggedright}m{4cm}ll}[hvlines]
Spin matrix & Eigenvalues & Eigenvectors\\
blah blah blah blah blah blah blah blah blah blah blah blah blah blah 
& $\frac{\hbar}{2},-\frac{\hbar}{2}$ & $\frac{1}{\sqrt{2}}%
\begin{bmatrix}
1\\
1
\end{bmatrix}
\qquad\frac{1}{\sqrt{2}}%
\begin{bmatrix}
-1\\
1
\end{bmatrix}
$\\
\end{NiceTabular}
\end{center}

\end{document}

您需要多次编译(因为nicematrix在后台使用 PGF/Tikz 节点)。

上述代码的输出

相关内容