环境在导入某些包时失败(longtable / multicol)

环境在导入某些包时失败(longtable / multicol)

如果我在序言中添加任何这些调用,我的环境就会失败:

  • \usepackage{pgfornament}
  • \usepackage{tikzsymbols}
  • \usepackage{xcolor}
  • \usepackage{tikz}
  • \hypersetup{colorlinks={true}}

其结果是:

  • Missing } inserted. \begin{myenvironment}
  • Extra }. or forgotten \endgroup. \end{myenvironment}
  • 奇怪的间距

问题有没有办法让环境与上述调用兼容?

问题您能否告诉我有关这个错误的背景信息以及如何处理它?


例子

该环境旨在平衡多列中的表格数据。

看,

和,

在此处输入图片描述 在此处输入图片描述

\documentclass[]{book}
\usepackage[T1]{fontenc}
\usepackage{longtable}
\usepackage{xparse}
\usepackage{multicol}
\usepackage{lipsum}
\usepackage[%
showframe,%
twoside,%
top=0.75in,%
bottom=0.75in,%
marginparsep=3ex,%
marginparwidth=0.8in,%
inner=0.75in,%
outer=1.25in,%
]{geometry}
\geometry{
    papersize={6in,4in}
}
\usepackage{hyperref}    % must be the last package
\hypersetup{%
    pdfauthor            = {MyAuthor},
    pdftitle             = {MyTitle},
    pdfsubject           = {MySubject},
    pdfkeywords          = {MyKeyword},
    linkcolor            = {magenta},
    urlcolor             = {blue},
    citecolor            = {cyan},
    filecolor            = {black},
%   colorlinks           = {true},  % causes Missing } inserted in myenvironment
}

% I was hoping to use these...
%\usepackage{pgfornament}        % causes Missing } inserted in myenvironment
%\usepackage{tikzsymbols}        % causes Missing } inserted in myenvironment

% I Don't really need these but they also break it
%\usepackage{xcolor}             % causes Missing } inserted in myenvironment
%\usepackage{tikz}               % causes Missing } inserted in myenvironment


% helpers for the test
\def\shortlipsum{\par Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis.\par}
\newcounter{entryno}
\setcounter{entryno}{1}
\def\tabline{Test & \the\value{entryno} & Description\addtocounter{entryno}{1}\\}
\def\tablines{\tabline\tabline\tabline\tabline\tabline\tabline\tabline\tabline\tabline}


% box for a multi column long table
% SEE https://tex.stackexchange.com/questions/161827/balanced-longtables-multicol-and-page-breaks
\makeatletter
\newsavebox\ltmcbox
\newsavebox\xxbox
\newenvironment{multicolslongtable}[1]{
    \setbox\ltmcbox\vbox\bgroup
    \col@number\@ne
    \begin{longtable}{#1}
}
{
    \end{longtable}
    \unskip
    \unpenalty
    \unpenalty
    \egroup
    \unvbox\ltmcbox
}
\makeatother


% environment to accept table input and produce multicol
\NewDocumentEnvironment{myenvironment}{O{2}}
{
    \savebox\xxbox
    \bgroup
    \begin{minipage}{\textwidth}
    \begin{multicols}{#1}
    \begin{multicolslongtable}{| l | l | l |}
}
{
    \end{multicolslongtable}
    \end{multicols}
    \end{minipage}
    \egroup
    \begin{multicols}{#1}
        \centering
        \usebox\xxbox
    \end{multicols}
}


% patch multicols to allow for one column
% SEE https://tex.stackexchange.com/questions/233866/one-column-multicol-environment
\let\multicolmulticols\multicols
\let\endmulticolmulticols\endmulticols
\RenewDocumentEnvironment{multicols}{mO{}}
{%
    \ifnum#1=1
    #2%
    \else % More than 1 column
    \multicolmulticols{#1}[#2]
    \fi
}
{%
    \ifnum#1=1
    \else % More than 1 column
    \endmulticolmulticols
    \fi
}


\begin{document}

\section{without `pgfornament` etc.}
\shortlipsum
\begin{myenvironment}
    \tablines
\end{myenvironment}
\shortlipsum

\end{document}

尝试 1:\bgroup --> \begingroup

交换\bgroup-->\begingroup\egroup-->\endgroup会导致输出丢失(Extra }、不当\prevdepth等等)


尝试 2:尝试平衡长表、多列和分页符再次

这个潜在的解决方案与软件包兼容,但我仍然无法将其重构到环境中。

问题有没有办法将其正确地重构到环境中?

当我复制/粘贴它(前两个表格)时它可以起作用,但我想避免这种情况。

环境产生

  • Undefined control sequence \begin{myenvironment}
  • Package longtable Error: longtable not in 1-column mode.
  • 未对齐的行
  • @numberne在应该有一行的位置添加了

在此处输入图片描述

\documentclass[]{book}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage{longtable}
\usepackage{xparse}
\usepackage{multicol}
\usepackage{lipsum}
\usepackage[%
showframe,%
twoside,%
top=0.75in,%
bottom=0.75in,%
marginparsep=3ex,%
marginparwidth=0.8in,%
inner=0.75in,%
outer=1.25in,%
]{geometry}
\geometry{
    papersize={6in,7in}
}
\usepackage{hyperref}    % must be the last package
\hypersetup{%
    pdfauthor            = {MyAuthor},
    pdftitle             = {MyTitle},
    pdfsubject           = {MySubject},
    pdfkeywords          = {MyKeyword},
    linkcolor            = {magenta},
    urlcolor             = {blue},
    citecolor            = {cyan},
    filecolor            = {black},
    colorlinks           = {true},  % no longer causes Missing } inserted
}

% no longer causes Missing } inserted
\usepackage{pgfornament}
\usepackage{tikzsymbols}
\usepackage{xcolor}
\usepackage{tikz}

% for testing
\def\shortlipsum{\par Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis.\par}
\newcounter{entryno}
\setcounter{entryno}{1}
\def\tabline{Test & \the\value{entryno} & Description\addtocounter{entryno}{1}\\}
\def\tablines{\tabline\tabline\tabline\tabline}


% patch multicols to allow for one column
% SEE https://tex.stackexchange.com/questions/233866/one-column-multicol-environment
\let\multicolmulticols\multicols
\let\endmulticolmulticols\endmulticols
\RenewDocumentEnvironment{multicols}{mO{}}
{%
    \ifnum#1=1
    #2%
    \else % More than 1 column
    \multicolmulticols{#1}[#2]
    \fi
}
{%
    \ifnum#1=1
    \else % More than 1 column
    \endmulticolmulticols
    \fi
}

% temporary box for multicols
\newsavebox\ltmcbox

% copy the working commands below into an environment
\NewDocumentEnvironment{myenvironment}{O{2}}
{
    \begin{multicols}{#1}
    \setbox\ltmcbox\vbox\bgroup
    \makeatletter\col@number\@ne
    \begin{longtable}{|l|l|l|}
}
{
    \end{longtable}
    \unskip
    \unpenalty
    \unpenalty
    \egroup
    \unvbox\ltmcbox
    \end{multicols}
}


\begin{document}

\shortlipsum

\begin{multicols}{2}
    \setbox\ltmcbox\vbox{
        \makeatletter\col@number\@ne
        \begin{longtable}{|l|l|l|}
            \tablines\tablines
        \end{longtable}
        \unskip
        \unpenalty
        \unpenalty
    }
    \unvbox\ltmcbox
\end{multicols}

\shortlipsum

\begin{multicols}{1}
    \setbox\ltmcbox\vbox{
        \makeatletter\col@number\@ne
        \begin{longtable}{|l|l|l|}
            \tablines\tablines
        \end{longtable}
        \unskip
        \unpenalty
        \unpenalty
    }
    \unvbox\ltmcbox
\end{multicols}

\shortlipsum

\begin{myenvironment}
    \tablines\tablines
\end{myenvironment}

\shortlipsum

\end{document}

其他考虑因素

supertabular

这个答案多列长表或枚举欺骗asupertabular在a中工作multicol,但我希望表格状的数据在一页上,在文本之间

tabbing

可能会有用,但是

  • 到目前为止,我的测试每行的平衡性还不够好,水平居中也不够好
  • 我希望每列都有格式,就像tabular
  • 我正在寻找由 LaTeX 计算的列宽

在此处输入图片描述

\documentclass[]{book}
\usepackage{xparse}
\usepackage{multicol}
\usepackage{lipsum}
\usepackage[%
showframe,%
twoside,%
top=0.5in,%
bottom=0.5in,%
marginparsep=3ex,%
marginparwidth=0.8in,%
inner=0.5in,%
outer=1.25in,%
]{geometry}
\geometry{
    papersize={6in,3in}
}

\def\shortlipsum{\par Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis.\par}

\def\entry{9000 \> Watt \> Power\\}

\usepackage{pgfornament}
\usepackage{tikzsymbols}
\usepackage{xcolor}
\usepackage{tikz}

\NewDocumentEnvironment{myenvironment}{O{2}}
{
\centering
\begin{multicols}{2}
\begin{tabbing}
\hskip4em \= \hskip4em \= \hskip16em \kill
}
{
\end{tabbing}
\end{multicols}
\par
}


\begin{document}

\shortlipsum

\begin{myenvironment}
\entry
\entry
\entry
\entry
\entry
\entry
\entry
\entry
\end{myenvironment}

\shortlipsum

\end{document}

list

列表可能会有用

  • 但我想要超过两列
  • 到目前为止,我的实验还没有很好地吻合

在此处输入图片描述

\documentclass[]{book}
\usepackage{xparse}
\usepackage{multicol}
\usepackage{lipsum}
\usepackage[%
showframe,%
twoside,%
top=0.5in,%
bottom=0.5in,%
marginparsep=3ex,%
marginparwidth=0.8in,%
inner=0.5in,%
outer=1.25in,%
]{geometry}
\geometry{
    papersize={6in,3in}
}

\def\shortlipsum{\par Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis.\par}


\usepackage{pgfornament}
\usepackage{tikzsymbols}
\usepackage{xcolor}
\usepackage{tikz}


\begin{document}
    
    \shortlipsum
    
    
    \newcommand{\entry}[2]{
        \item[#1] #2
    }
    
    \begin{multicols}{2}
        \centering
        \begin{description}
            \entry{1}{2}
            \entry{1}{2}
            \entry{1}{2}
            \entry{1}{2}
            \entry{1}{2}
            \entry{1}{2}
            \entry{1}{2}
        \end{description}
    \end{multicols}
    
    \shortlipsum
    
\end{document}

相关内容