TransWikia.com

How to conditionally display in bold a R generated p-value with the symbol < (<0.001 or < 0.001)?

TeX - LaTeX Asked by CharlesLDN on August 1, 2021

I have LaTeX tables being automatically generated (populated) via R (Rstudio, Knitr), and compiled with PdfLaTeX (MiKTeX). There are rows of p-values for each statistic test.

I am using this newcommand from the fp package (Modify LaTeX table p-values by multiple conditions)

newcolumntype{P}{>{collectcellpval}c<{endcollectcell}}
newcommandpval[1]{FPiflt{#1}{0.5}textbf{#1}else#1fi}

to display the p-values in bold text if they are below 0.05.
The example below which is a mix of Latex and R works fine

pval{Sexpr{format.pval (round (crr_1 $ coef ["gender",]["p-value"], 2), digits = 3, eps = 0.001, nsmall = 3)}}

and produces 0.040.

However, when the results of the format.pval R function gives the value <0.001, it does not work and produces the following message: FP error: Illegal character < found in float number !.

I have the same problem when I use the package Publish as displayed in the example below.

pval{Sexpr{publish (csc_1, print = F) [[2]] $ regressionTable $ p [[1]]}}

How can I circumvent this problem by using the FPiflt function or by another way to produce bold p-value of <0.001 ?

Thank you in advance for your help.

2 Answers

You can try the following version.

documentclass{article}
usepackage{expl3}

ExplSyntaxOn

newcommand{pval}[1]{
    tl_set:Nn l_tmpa_tl {#1}
    tl_set:Nn l_tmpb_tl {#1}
    % remove all special characters
    regex_replace_all:nnN {[<>=]} {} l_tmpa_tl
    fp_set:Nn l_tmpa_fp {l_tmpa_tl}
    fp_compare:nTF {l_tmpa_fp <= 0.5} {
        group_begin: bfseries tl_use:N l_tmpb_tl group_end:
    } {
        tl_use:N l_tmpb_tl
    }
}

ExplSyntaxOff

begin{document}

pval{0.8}
pval{<0.001}

end{document}

Answered by Alan Xiang on August 1, 2021

The code below updates pval to condition on the presence of < within the captured cell/argument.

enter image description here

documentclass{article}

usepackage[T1]{fontenc}
usepackage{array,collcell}
usepackage{xfp}

newcolumntype{P}{>{collectcellpval}c<{endcollectcell}}
makeatletter
newcommandpval[1]{%
  defpval@{#1}% Store value
  % patchcmd{<cmd>}{<search>}{<replace>}
  patchcmd{pval@}{<}{}
    {ifnumfpeval{pval@ < 0.5}=1
      expandaftertextbf
     fi
    {<pval@}}% <success> - < was present
    {ifnumfpeval{pval@ < 0.5}=1
      expandaftertextbf
     fi
    {pval@}}% <failure> - no < present
}

begin{document}

begin{tabular}{ | P | }
  0.4 
  0.6 
  0.1 
  0.01 
  0.001 
  <0.001
end{tabular}

end{document}

Answered by Werner on August 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP