使用 tabularx 创建新的词汇表样式

使用 tabularx 创建新的词汇表样式

我正在使用词汇表包我正在尝试创建自己的词汇表打印样式。具体来说,我希望我的词汇表出现在特定格式的 tabularx 表中。我一直在尝试参考文档,但它们并没有详细介绍这一点,我能找到的唯一其他资源是这个答案现在看来已经过时了,因为即使提供的答案对我来说也不起作用。

最终,下面的 MWE 中的内容因 \noalign 放错位置而失败。有人能告诉我如何使用我想要的表格格式创建词汇表吗?

最小(非)工作示例

\documentclass{article}

\usepackage[table]{xcolor}      % Provides coloring for tables and text
\usepackage{tabularx}           % Customized table formatting
\usepackage{environ}        % Used to define custom table environment
\usepackage[acronym]{glossaries}

% Define table related commands and properties
\definecolor{greyblue}{rgb}{0.6353,0.6863,0.7686} % Define a color used in the tables
\newcommand\setrow[1]{\gdef\rowmac{#1}#1\ignorespaces} % Used for making a row bold
\newcommand\clearrow{\global\let\rowmac\relax} \clearrow % Used for clearing a row formatting
\newcolumntype{C}[1]{>{\hsize=#1\hsize\rowmac\centering\arraybackslash}X} % Centered column, input is relative width of page
\newcolumntype{L}[1]{>{\hsize=#1\hsize\rowmac\raggedright\arraybackslash}X} % Left-aligned column, input is relative width of page
\newcolumntype{R}[1]{>{\hsize=#1\hsize\rowmac\raggedleft\arraybackslash}X} % Left-aligned column, input is relative width of page

\makenoidxglossaries

\newglossarystyle{docstyle}
{%
    \renewenvironment{theglossary}
    {%
        \table[!htbp]
        \centering
        \rowcolors{2}{black!5}{black!15}
        \tabularx{\linewidth}{C{0.25}|L{0.75}<{\clearrow}}%
        \hline
        \rowcolor{greyblue} \setrow{\bfseries} % Make the header row bold and colored grey-blue
    }{
        \hline
        \endtabularx
        \caption{Abbreviations}
        \label{tbl:abbrev}
        \endtable
    }
    \renewcommand*{\glossaryheader}{}%
    % Don't do anything between letter groups
    \renewcommand*{\glsgroupheading}[1]{}%
    \renewcommand*{\glsgroupskip}{}%
    % Set display for each the acronym entry
    \renewcommand{\glossentry}[2]{%
        \glstarget{##1}{\glsentryshort{##1}}% short form
        &
        \glsentrylong{##1}% long form
        \\% end of row
    }%
}

\newacronym{ABC}{ABC}{Alphabet}

\begin{document}
    \gls{ABC}
    \gls{ABC}

    \printnoidxglossary[type=\acronymtype,style=docstyle]
\end{document}

答案1

经过一番尝试,我找到了一个适合我的答案。事实证明,上述代码的问题在于\hline关闭环境中。如果将其删除,问题就解决了,表格也创建得很好。然而,这意味着我无法像我想要的那样得到表格下方的行。

为了使表格格式完全符合我的要求,我必须使用该environ包。这样我就可以创建一个按我想要的方式工作的漂亮表格环境,并且该表格环境可用于定义词汇表样式。下面是一个完全符合我要求的 MWE。

\documentclass{article}

\usepackage[table]{xcolor}      % Provides coloring for tables and text
\usepackage{tabularx}           % Customized table formatting
\usepackage{environ}        % Used to define custom table environment
\usepackage[acronym]{glossaries}

% Define table related commands and properties
\definecolor{greyblue}{rgb}{0.6353,0.6863,0.7686} % Define a color used in the tables
\newcommand\setrow[1]{\gdef\rowmac{#1}#1\ignorespaces} % Used for making a row bold
\newcommand\clearrow{\global\let\rowmac\relax} \clearrow % Used for clearing a row formatting
\newcolumntype{C}[1]{>{\hsize=#1\hsize\rowmac\centering\arraybackslash}X} % Centered column, input is relative width of page
\newcolumntype{L}[1]{>{\hsize=#1\hsize\rowmac\raggedright\arraybackslash}X} % Left-aligned column, input is relative width of page
\newcolumntype{R}[1]{>{\hsize=#1\hsize\rowmac\raggedleft\arraybackslash}X} % Left-aligned column, input is relative width of page

\makenoidxglossaries


\NewEnviron{doctable}[4]{%
    \begin{table}[!htbp]
        \centering
        \rowcolors{2}{black!5}{black!15}
        \begin{tabularx}{#3\linewidth}{#4<{\clearrow}}%
            \hline
            \rowcolor{greyblue} \setrow{\bfseries} % Make the header row bold and colored grey-blue
            \BODY
            \hline
        \end{tabularx}
        \caption{#2}
        \label{#1}
    \end{table}
}

\newglossarystyle{docstyle}
{%
    \renewenvironment{theglossary}
    {%
        \doctable
        {tbl:abbrev}
        {Abbreviations}
        {1} { L{0.2}|L{0.8} }
        Abbreviation & Description \\
    }{
        \enddoctable
    }
    \renewcommand{\glossarysection}[2][]{}
    \renewcommand*{\glossaryheader}{}%
    % Don't do anything between letter groups
    \renewcommand*{\glsgroupheading}[1]{}%
    \renewcommand*{\glsgroupskip}{}%
    % Set display for each the acronym entry
    \renewcommand{\glossentry}[2]{%
        \glstarget{##1}{\glsentryshort{##1}}% short form
        &
        \glsentrylong{##1}% long form
        \\% end of row
    }%
}

\newacronym{ABC}{ABC}{Alphabet}
\newacronym{GHI}{GHI}{test}
\newacronym{DEF}{DEF}{Some other acronym}

\begin{document}
    \gls{ABC}
    \gls{ABC}
    \gls{DEF}

    \printnoidxglossary[type=\acronymtype,style=docstyle]
\end{document}

得出的结果为:

在此处输入图片描述

相关内容