Regular editing of latex tables

I have a LaTeX table (below is a snippet) that contains two values ​​(value 1: standard error, value 2: evaluation) after each \ underset. I would like to edit by adding \ bf {} to the second value after each \ underset if the second value divided by the first value is greater than 2 or less than -2.

For example, after the first \ underset (after "interception") -9.686 / 1.742 is <-2, so I want the original {-9.686} to read as \ bf {-9.686}. I have hundreds of variables, so manual transition is not preferred. I only work with Notepad ++, but if necessary, I can try Python. Any advice for regular expression and / or a suitable platform (editor / parser, etc.) for a PC would be much appreciated.

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} 

I would like the output table to read like this.

Output table:

 \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} 
+4
source share
2 answers

This can be done completely in LaTeX ...

enter image description here

 \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} 

In the above example, both tables are the same with the top using the standard c -column specification, and the bottom uses the new c -column specification. The latter intercepts the \underset setting to change the font of the second argument.

collcell package is used to capture each cell content in columns 2-8. They must exactly match the form $\underset{#1}{#2}$ so that it can be converted to $\underset{#1}{\mathbf{#2}}$ . The array package simplifies the collection of cells through a new column (simplifies the code).

Some notes:

  • I used the replication column specifier *{<num>}{<col spec>} , which repeats the specification of the <col spec> column only <num> times. This makes the code more readable.

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

  • Instead of duplicating the same primitive concept of over-and-over, define a macro that you can use (see Consistent typography ). Thus, it gives you a hook into the contents, which you can easily manipulate if you change your mind. For instance,

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

    and then use \stderrest{<stderr>}{<est>} wherever you need this set of things. Thus, formatting updates are as easy as

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

    and you're done.

  • Do not use the center environment inside your float; rather use \centering . As a link, see Should I use \center or \centering for numbers and tables?

  • Using the pair \makeatletter ... \makeatother is only that I have defined a specific β€œinternal” command that matches the parameter you entered. This is optional and subject to change. But for more information on this, see What do \makeatletter and \makeatother do?

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


... is there anyway to add a conditional operator that only inserts \mathbf to {#2} if and only if the absolute value {#2} divided by {#1} is greater than 2? At the moment, he is adding \mathbf to each value in {#2} .

For conditional formatting of a variable, here is one parameter:

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 provides fixed point arithmetic for the condition of the value of certain variables. I am sure that the updated code is self-explanatory.

+5
source

Python example:

 #!/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) 

In short, the idea is to capture two values ​​to go to a lambda function that compares the absolute value of a quotient with 2 and returns a modified string or a whole match.

+1
source

Source: https://habr.com/ru/post/1495063/


All Articles