如何在词汇表包中使用 xltabular 环境?

如何在词汇表包中使用 xltabular 环境?

我使用 MWE 中的环境定义了 glossarystyle longtable

\documentclass{article}
\usepackage[nopostdot,nonumberlist,acronym]{glossaries-extra}

\renewcommand{\newacronym}[3]{%
    \newglossaryentry{#1}{%
        type=acronym, 
        name=#2,
        description={#3},
        first={{#3} ({#2})},
    }
}

\newglossarystyle{acronym}
{%
    \setglossarystyle{long}
    \renewenvironment{theglossary}%
 {\begin{longtable}[l]{p{0.13\textwidth}p{0.05\textwidth}p{0.82\textwidth}}}%
          {\end{longtable}}
    \renewcommand{\glossentry}[2]{%
        \glsentryitem{##1}\glstarget{##1}{\glossentryname{##1}} &
        — & \glossentrydesc{##1}\glspostdescription\space ##2\tabularnewline
    }%
}


\newacronym{PNG}{PNG}          {Portable Network Graphics }
\newacronym{SVG}{SVG}        {Scalable Vector Graphics}
\newacronym{JPEG}{JPEG}        {Joint Photographic Experts Group}

\makeglossaries

\begin{document}

    \glsaddall
    \printglossary[type=acronym,style=acronym,title={}] 

    First use of \gls{JPEG} and \gls{PNG}. Second use of \gls{PNG} and \gls{JPEG}.

\end{document}

而且效果很好。有一天,我决定将其更改longtablexltabular,如下所示:

\documentclass{article}
\usepackage[nopostdot,nonumberlist,acronym]{glossaries-extra}
\usepackage{xltabular}


\renewcommand{\newacronym}[3]{%
    \newglossaryentry{#1}{%
        type=acronym, 
        name=#2,
        description={#3},
        first={{#3} ({#2})},
    }
}

\newglossarystyle{acronym}
{%
    \setglossarystyle{long}
    \renewenvironment{theglossary}%
    {\begin{xltabular}{\textwidth}{|l|c|X|}}%
        {\end{xltabular}}%
    \renewcommand{\glossentry}[2]{%
        \glsentryitem{##1}\glstarget{##1}{\glossentryname{##1}} &
        — & \glossentrydesc{##1}\glspostdescription\space ##2\tabularnewline
    }%
}


\newacronym{PNG}{PNG}          {Portable Network Graphics }
\newacronym{SVG}{SVG}        {Scalable Vector Graphics}
\newacronym{JPEG}{JPEG}        {Joint Photographic Experts Group}

\makeglossaries

\begin{document}

    \glsaddall
    \printglossary[type=acronym,style=acronym,title={}] 

    First use of \gls{JPEG} and \gls{PNG}. Second use of \gls{PNG} and \gls{JPEG}.

\end{document}

我遇到了一些错误:

File ended while scanning use of \TX@get@body. ...lossary[type=acronym,style=acronym,title={}]

\begin{xltabular} on input line 2 ended by \end{document}. \end{document}

longtable我认为,错误是由和中的非可选参数数量不同引起的xltabular,但无法找到解决此问题的线索。

答案1

如果您有一个表格/词汇表需要跨越多个页面,并且希望能够通过X在表格中使用来动态调整列宽,那么xltabular这是一个非常好的选择。它也为我解决了这个问题!

遗憾的是,无法在新环境定义中使用\begin{xltabular}和。不过,您可以使用和。\end{xltabular}\xltabular{\linewidth}{...}\endxltabular

这是一个具有可变列宽的三列词汇表样式:

% --------------------------------------------------------------------------------
% Style of the Symbols List
% --------------------------------------------------------------------------------
\newglossarystyle{symbstyle} {
    \setglossarystyle{long3col}  % base this style on the list style
    \renewenvironment{theglossary} {
        % Change the table type --> 3 columns
        \xltabular{\linewidth}{p{0.2\textwidth}Xp{0.1\textwidth}}
    }{
        \endxltabular
    }

    %  Change the table header / footer
    \renewcommand*{\glossaryheader} {
        \bfseries Head1 & \bfseries Head2 & \bfseries Head3 \\
        \hline \endfirsthead
        \hline \endfoot
    }

    % Change the displayed items
    \renewcommand*{\glossentry}[2] {
        \glstarget{##1}{\glossentryname{##1}} & \glossentrydesc{##1} & \glspostdescription{##1} \\
        %               Head1                        Head2                 Head3
    }
}

希望这能帮助到别人!

答案2

谢谢这个答案我找到解决办法了。

\documentclass{article}
\usepackage[nopostdot,nonumberlist,acronym]{glossaries-extra}
\usepackage{xltabular}
\usepackage{environ}        % Used to define custom table environment

\renewcommand{\newacronym}[3]{%
    \newglossaryentry{#1}{%
        type=acronym, 
        name=#2,
        description={#3},
        first={{#3} ({#2})},
    }
}

\NewEnviron{doctable}{%
    \begin{xltabular}{\textwidth}{lcX}%
        \BODY
    \end{xltabular}
}

\newglossarystyle{acronym}
{%
    \setglossarystyle{long}
    \renewenvironment{theglossary}
{%
    \doctable
}{
    \enddoctable
}
    \renewcommand{\glossentry}[2]{%
        \glsentryitem{##1}\glstarget{##1}{\glossentryname{##1}} &
        — & \glossentrydesc{##1}\glspostdescription\space ##2\tabularnewline
    }%
}


\newacronym{PNG}{PNG}          {Portable Network Graphics }
\newacronym{SVG}{SVG}        {Scalable Vector Graphics}
\newacronym{JPEG}{JPEG}        {Joint Photographic Experts Group}

\makeglossaries

\begin{document}

    \glsaddall
    \printglossary[type=acronym,style=acronym,title={}] 

    First use of \gls{JPEG} and \gls{PNG}. Second use of \gls{PNG} and \gls{JPEG}.

\end{document}

相关内容