编辑Latex表格的正则表达式

4
我有一个LaTeX表格(下面是一段摘录),每个\underset之后都包含两个值(值1:标准误差;值2:估计值)。只要第二个值除以第一个值大于2或小于-2,我就想通过在每个\underset之后将\bf{}添加到第二个值来进行编辑。
例如,在第一个\underset之后('intercept'之后),-9.686/1.742 < -2,因此我希望原始的{-9.686}变为\bf{-9.686}。我有数百个变量,因此不建议手动操作。我只使用notepad ++,但如果必要,可以尝试Python。非常感谢正则表达式和/或适用于PC的平台(编辑器/解析器等)的任何提示。
documentclass[letter]{article}
\usepackage{amsmath, amsthm, amssymb}  % Additional math packages

\begin{document}

\begin{table}[hptb]
\begin{center}
\begin{tabular}{|l||c|c|c|c|c|c|c|} \hline

(Intercept)&$\underset{(1.742)}{-9.686}$&$\underset{(0.907)}     
{-9.085}$&$\underset{(0.663)}{-9.724}$&$\underset{(0.565)}{-9.077}$&$\underset{(0.619)} 
{-7.601}$&$\underset{(0.838)}{-5.273}$&$\underset{(1.120)}{-3.417}$\\ 
\end{tabular}
\end{center}
\end{table}
\end{document}

我希望输出表格如下所示。

输出表格:

\begin{document}

\begin{table}[hptb]
\begin{center}
\begin{tabular}{|l||c|c|c|c|c|c|c|} \hline

(Intercept)&$\underset{(1.742)}{\bf{-9.686}}$&$\underset{(0.907)}
{\bf{-9.085}}$&$\underset{(0.663)}{-9.724}$&$\underset{(0.565)}{\bf{-
-9.077}}$&$\underset{(0.619)}{\bf{-7.601}}$&$\underset{(0.838)} 
{-5.273}$&$\underset{(1.120)}{-1.417}$\\ 
\end{tabular}
\end{center}
\end{table}
\end{document}

你可以忘记Notepad++了。 - Casimir et Hippolyte
2个回答

5
这可以完全在LaTeX中完成...

这是图片描述

\documentclass{article}
\usepackage{amsmath,collcell,array}% http://ctan.org/pkg/{amsmathm,collcell,array}
\makeatletter
\def\insertbf@$\underset#1#2${$\underset{#1}{\mathbf{#2}}$}
\newcommand{\insertbf}[1]{\expandafter\insertbf@#1}
\newcolumntype{C}{>{\collectcell\insertbf}c<{\endcollectcell}}
\makeatother
\begin{document}

\begin{table}[hptb]
  \centering
  \begin{tabular}{|l||*{7}{c|}} \hline
    (Intercept) & $\underset{(1.742)}{-9.686}$ & $\underset{(0.907)}{-9.085}$ & $\underset{(0.663)}{-9.724}$
      & $\underset{(0.565)}{-9.077}$ & $\underset{(0.619)}{-7.601}$ & $\underset{(0.838)}{-5.273}$ 
      & $\underset{(1.120)}{-3.417}$
  \end{tabular}

  \bigskip

  \begin{tabular}{|l||*{7}{C|}} \hline
    (Intercept) & $\underset{(1.742)}{-9.686}$ & $\underset{(0.907)}{-9.085}$ & $\underset{(0.663)}{-9.724}$
      & $\underset{(0.565)}{-9.077}$ & $\underset{(0.619)}{-7.601}$ & $\underset{(0.838)}{-5.273}$ 
      & $\underset{(1.120)}{-3.417}$
  \end{tabular}  
\end{table}
\end{document}

在上面的例子中,两个表格都相同,顶部使用常规的c列规范,而底部使用新定义的C列规范。后者拦截了\underset的设置,以修改第二个参数的字体。
使用collcell捕获第2-8列的每个单元格内容。它们必须完全匹配$\underset{#1}{#2}$的形式,以便可以将其转换为$\underset{#1}{\mathbf{#2}}$。通过新定义的列(简化代码),array简化了单元格收集。
需要注意以下几点:
  • I used a replication column specifier *{<num>}{<col spec>} which repeats column specification <col spec> a total of <num> times. It makes for more readable code.

  • Use \mathbf instead of the text-related \bf. See Does it matter if I use \textit or \it, \bfseries or \bf, etc. and Will two-letter font style commands (\bf, \it, …) ever be resurrected in LaTeX?

  • Rather than duplicating the same primitive concept over-and-over, define a macro that you can use (see Consistent typography). This way it gives you a hook into the contents that you can easily manipulate if you change your mind. For example,

    \newcommand{\stderrest}[2]{$\underset{(#1)}{#2}$}% \stderrest{<stderr>}{<est>}
    

    and then use \stderrest{<stderr>}{<est>} wherever you need this kind of thing set. That way, an update to the formatting is as easy as

    \renewcommand{\stderrest}[2]{$\underset{(#1)}{\mathbf{#2}}$}% \stderrest{<stderr>}{<est>}
    

    and you're done.

  • Don't use the center environment inside your float; rather use \centering. As reference, see Should I use \center or \centering for figures and tables?

  • The use of the \makeatletter...\makeatother pair is just because I defined a specific "internal" command that matches the input of your specification. It's not really necessary and can be changed. But for more information on this, see What do \makeatletter and \makeatother do?

  • If you have a cell in the newly-defined C-column that does not have an $\underset{#1}{#2}$ form, you need to use \multicolumn{1}{c|}{..} to avoid it being collected.


… 有没有办法添加一个条件语句,只在绝对值 {#2} 除以 {#1} 大于2时,在 {#2} 之前插入 \mathbf?目前,它将 \mathbf 添加到 {#2} 中的每个值。

针对变量的条件格式化,这里有一个选项:

enter image description here

\documentclass{article}
\usepackage{amsmath,collcell,array}% http://ctan.org/pkg/{amsmathm,collcell,array}
\usepackage[nomessages]{fp}% http://ctan.org/pkg/fp
\makeatletter
\let\stderr@format\relax
\def\stripparen@(#1){#1}% Remove parentheses
\def\insertbf@$\underset#1#2${%
  \FPabs\resultA{#2}% Absolute value of #2
  \edef\resultB{\stripparen@#1}% Extract std err
  \FPeval\result{\resultA / \resultB}% Divide est / std err
  \FPifgt\result{2}\let\stderr@format\mathbf\fi%
  $\underset{#1}{\stderr@format{#2}}$}%
\newcommand{\insertbf}[1]{\expandafter\insertbf@#1}
\newcolumntype{C}{>{\collectcell\insertbf}c<{\endcollectcell}}
\makeatother
\begin{document}

\begin{table}[hptb]
  \centering
  \begin{tabular}{|l||*{7}{c|}} \hline
    (Intercept) & $\underset{(1.742)}{-9.686}$ & $\underset{(0.907)}{-9.085}$ & $\underset{(0.663)}{-9.724}$
      & $\underset{(0.565)}{-9.077}$ & $\underset{(0.619)}{-7.601}$ & $\underset{(0.838)}{-5.273}$ 
      & $\underset{(1.120)}{-3.417}$
  \end{tabular}

  \bigskip

  \begin{tabular}{|l||*{7}{C|}} \hline
    (Intercept) & $\underset{(1.742)}{-9.686}$ & $\underset{(0.907)}{-9.085}$ & $\underset{(0.663)}{-9.724}$
      & $\underset{(0.565)}{-9.077}$ & $\underset{(0.619)}{-7.601}$ & $\underset{(0.838)}{-5.273}$ 
      & $\underset{(1.120)}{-3.417}$
  \end{tabular}  

  \bigskip

  \begin{tabular}{|l||*{7}{C|}} \hline
    (Intercept) & $\underset{(1.742)}{-2.686}$ & $\underset{(0.907)}{-9.085}$ & $\underset{(0.663)}{-9.724}$
      & $\underset{(0.565)}{-9.077}$ & $\underset{(0.619)}{-7.601}$ & $\underset{(0.838)}{-1.273}$ 
      & $\underset{(1.120)}{-3.417}$
  \end{tabular}  
\end{table}
\end{document}

fp提供了固定点算术,用于调整某些变量的值。我相信更新后的代码已经很容易理解了。


这很棒,是否有任何方法添加一个条件语句,仅在 {#2} 的绝对值除以 {#1} 大于2时才在 {#2} 前插入 \mathbf? - Anthony Howell
目前,它将\mathbf添加到{#2}中的每个值。谢谢。 - Anthony Howell

1
一个用Python编写的示例:
#!/usr/bin/python

import re

data = r'''documentclass[letter]{article}
\usepackage{amsmath, amsthm, amssymb}  % Additional math packages

\begin{document}

\begin{table}[hptb]
\begin{center}
\begin{tabular}{|l||c|c|c|c|c|c|c|} \hline

(Intercept)&$\underset{(1.742)}{-9.686}$&$\underset{(0.907)}     
{-9.085}$&$\underset{(0.663)}{-9.724}$&$\underset{(0.565)}{-9.077}$&$\underset{(0.619)} 
{-7.601}$&$\underset{(0.838)}{-5.273}$&$\underset{(1.120)}{-3.417}$\\ 
\end{tabular}
\end{center}
\end{table}
\end{document}'''

print re.sub(r'(\\underset{\()(-?[0-9]+(?:\.[0-9]+)?)(\)}\s*{)(-?[0-9]+(?:\.[0-9]+)?)(?=}\$)',
    lambda m: m.group(1)+m.group(2)+m.group(3)+'\\bf{'+m.group(4)+'}'
        if abs(float(m.group(4))/float(m.group(2)))>2 else m.group(0), data)

简而言之,这个想法是捕获两个值传递给lambda函数,该函数将绝对值的商与2进行比较,并返回修改后的字符串或整个匹配。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接