查找 \bf 文本的宽度以调整表格中段落的大小

查找 \bf 文本的宽度以调整表格中段落的大小

我有这个结构,它构建了一个与文本宽度完全相同的表格,分为两列。我希望左列的宽度与列中最长的单词的宽度完全相同,但仍然是一个段落(而不是对齐 l)。

\widthof{Longest Text in Column}}目前正在使用,但是当我想使用粗体字体时它就中断了。

正常运行和故障示例:

\documentclass[final,openbib,oneside,12pt]{memoir}
\usepackage{tabularx}
\usepackage{lipsum}
\usepackage{calc}

\begin{document}
    \noindent
    {\Large This Page Works:}\\\\
    \hspace*{-6pt}\begin{tabularx}{\linewidth+12pt}{p{\widthof{Army Group Centre}}|X}
        Army \hfill Group \hfill North \par 
        \vspace{\baselineskip} 
        More text and stuff will go in the paragraph, that's why it's a
        paragraph instead of ``l''
        & \lipsum[2]\\
        Army Group Centre & \lipsum[2]\\
        Army \hfill Group \hfill South & \lipsum[2]\\
    \end{tabularx}
    {\Large This Page Does Not:}\vspace{.5\baselineskip}\\
    %% trying to measure bold font causes error:
    %
    % \hspace*{-6pt}\begin{tabularx}{\linewidth+12pt}{p{\widthof{\textbf{Army Group Centre}}}|X}
    %
    %% measuring without bold font causes linewrap:
    \hspace*{-6pt}\begin{tabularx}{\linewidth+12pt}{p{\widthof{Army Group Centre}}|X}
        \textbf{Army \hfill Group \hfill North} \par 
        \vspace{\baselineskip} 
        More text and stuff will go in the paragraph, that's why it's a
        paragraph instead of ``l''
        & \lipsum[2]\\
        \textbf{Army Group Centre} & \lipsum[2]\\
        \textbf{Army \hfill Group \hfill South} & \lipsum[2]
    \end{tabularx}
\end{document}

答案1

您可以定义 a \\newlength{\WidestLength},然后将长度存储为:

\settowidth{\WidestLength}{\bfseries Army Group Center}

在此处输入图片描述

笔记:

  • 无需猜测\hspace*{-6pt}+12pt。您可以简单地通过删除在表格开头和结尾添加的列填充@{}
  • showframe 仅用于显示页边距。实际使用中不需要它。

代码:

\documentclass[final,openbib,oneside,12pt]{memoir}
\usepackage{tabularx}
\usepackage{lipsum}
\usepackage{showframe}

\newlength{\WidestLength}

\begin{document}
    \noindent
    {\Large\noindent This Page Works:}
    
    \settowidth{\WidestLength}{Army Group Center}
    \noindent
    \begin{tabularx}{\linewidth}{@{}p{\WidestLength}|X@{}}
        Army \hfill Group \hfill North \par 
        \vspace{\baselineskip} 
        More text and stuff will go in the paragraph, that's why it's a
        paragraph instead of ``l''
        & \lipsum[2]\\
        Army Group Centre & \lipsum[2]\\
        Army \hfill Group \hfill South & \lipsum[2]\\
    \end{tabularx}
    
    {\Large\noindent This Page Now Also Works:}
    
    %% trying to measure bold font causes error:
    %
    % \hspace*{-6pt}\begin{tabularx}{\linewidth+12pt}{p{\widthof{\textbf{Army Group Centre}}}|X}
    %
    %% measuring without bold font causes linewrap:
    \settowidth{\WidestLength}{\bfseries Army Group Center}
    \noindent\begin{tabularx}{\linewidth}{@{}p{\WidestLength}|X@{}}
        \textbf{Army \hfill Group \hfill North} \par 
        \vspace{\baselineskip} 
        More text and stuff will go in the paragraph, that's why it's a
        paragraph instead of ``l''
        & \lipsum[4]\\
        \textbf{Army Group Centre} & \lipsum[4]\\
        \textbf{Army  Group \hfill South} & \lipsum[4]
    \end{tabularx}
\end{document}

相关内容