Call a single cell of LaTeX Table - latex

I would like to call a single value from a table inside a textbody.
% latex table generated in R 3.5.1 by xtable 1.8-3 package
% Wed Dec 12 13:56:07 2018
\begin{table}[ht]
\centering
\begin{tabular}{rrrrr}
\hline
& Estimate & Std. Error & z value & Pr($>$$|$z$|$) \\
\hline
(Intercept) & -1.819 & 0.926 & -1.964 & 0.050 \\
hiveH2 & 2.418 & 0.951 & 2.542 & 0.011 \\
nectar+ & 0.827 & 0.947 & 0.873 & 0.383 \\
\hline
\end{tabular}
\label{tab:x}
\end{table}
I would like to be able to write something along the line:
The z value of Hive~2 is \call{tab:x{z value}{hiveH2}}.
That should compile as:
The z value of Hive 2 is 2.542.
the LaTeX table was generated with a R script (neither R markdown nor R sweave):
library(lme4)
library(xtable)
data <- matrix(c("H1","H1","H2","H2","H1","H1","H2","H2","H1","H1","H2","H2","H1","H1","H2","H2","H1","H1","H2","H2","H1","H1","H2","H2","H1","H1","H2","H2",
"+","+","-","-","+","+","-","-","+","+","-","-","+","+","-","-","-","-","+","+","-","-","+","+","-","-","+","+",
0.00000000,0.00000000,0.25000000,0.00000000,0.00000000,0.00000000,0.00000000,0.10000000,0.20000000,0.00000000,0.10000000,0.00000000,0.00000000,0.00000000,0.00000000,0.09090909,0.09090909,0.00000000,0.41666667,0.08333333,0.00000000,0.14285714,0.85714286,0.61538462,0.00000000,0.00000000,0.25000000,1.10000000),
nrow=28, ncol=3)
data <- as.data.frame(data)
colnames(data) <- c("hive", "nectar", "severity")
glm <- glm(severity ~ hive + nectar, data = data, family = binomial)
summary.glm <- summary(glm)
summary.glm
print(xtable(summary.glm, type = "latex", caption.placement = getOption("xtable.caption.placement", "bottom"),
digits = 3,
label = "tab:x",
table.placement = getOption("xtable.table.placement", "h")),
file = "test.tex")

Related

Latex with both a thousand separator comma and decimal point [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 days ago.
Improve this question
I am trying to include both a decimal point and comma separator in a table. Even though the table input includes both the commas and the decimal separators, the latex table converts all the comma separating the thousands digits to decimal points. Is there a way to have both at the same time and align the numbers at the decimal and comma separators?
Thank you!
\documentclass{wptemp}
\usepackage{adjustbox}
\usepackage{setspace}\doublespacing
\usepackage{booktabs}
\usepackage{siunitx}
\newcolumntype{d}{S[input-symbols = ()]}
\usepackage{lscape}
\newcommand{\source}[1]{\caption*{Source: {#1}} }
% *****************************************************************
% Estout LaTeX wrapper
% *****************************************************************
%%Original code developed by Jörg Weber: see
%% https://www.jwe.cc/2012/03/stata-latex-tables-estout/
%% and
%% https://www.jwe.cc/blog/
\let\estinput=\input % define a new input command so that we can still flatten the document
\newcommand{\estwide}[3]{
\vspace{.75ex}{
%\textsymbols% Note the added command here
\begin{tabular*}
{\textwidth}{#{\hskip\tabcolsep\extracolsep\fill}l*{#2}{#3}}
\toprule
\estinput{#1}
\bottomrule
\addlinespace[.75ex]
\end{tabular*}
}
}
\newcommand{\estauto}[3]{
\vspace{.75ex}{
%\textsymbols% Note the added command here
\begin{tabular}{l*{#2}{#3}}
\toprule
\estinput{#1}
\bottomrule
\addlinespace[.75ex]
\end{tabular}
}
}
% Allow line breaks with \ in specialcells
\newcommand{\specialcell}[2][c]{%
\begin{tabular}[#1]{#{}c#{}}#2\end{tabular}
}
\newcommand{\sym}[1]{\rlap{#1}}% Thanks David Carlisle
%%%%%%%%%%% End of wrapper %%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%% TiKz %%%%%%%%%%%%%%%%%%%%%
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}
\tikzstyle{startstop} = [rectangle, rounded corners, minimum width=3cm, minimum height=1cm,text centered, draw=black, fill=red!30]
\tikzstyle{io} = [trapezium, trapezium left angle=70, trapezium right angle=110, minimum width=3cm, minimum height=1cm, text centered, text width =5cm, draw=black, fill=blue!30]
\tikzstyle{process} = [rectangle, minimum width=3cm, minimum height=1cm, text centered, text width =5cm, draw=black, fill=orange!30]
\tikzstyle{decision} = [diamond, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill=green!30]
\tikzstyle{arrow} = [thick,->,>=stealth]
\begin{document}
\linespread{1}{\title{{\fontfamily{ptm}\selectfont{\Large{\MakeUppercase{}}}}
% \thanks{}
}}
\author{}
\date{\fontfamily{pbk}\selectfont\normalsize{\textsc{\today}}}
\maketitle
% \begin{abstract}
% % \keywords{Keyword1; Keyword2; Keyword3.}
% % \JEL{}
% \end{abstract}
\thispagestyle{empty}
\pagenumbering{arabic}
\begin{table}[H]
\caption{Spill Over Effect of depression on GPA \label{tab:reggpa}}
\centering
\resizebox{\linewidth}{!}{
\begin{tabular}[t]{ldddddd}
\toprule
\multicolumn{1}{c}{Dependent Variable} & \multicolumn{1}{c}{Own Depression} & \multicolumn{1}{c}{GPA} & \multicolumn{1}{c}{Skip School} & \multicolumn{1}{c}{Miss School} & \multicolumn{1}{c}{Smoke Cigarettes} & \multicolumn{1}{c}{Get Drunk} \\
\cmidrule(l{3pt}r{3pt}){1-1} \cmidrule(l{3pt}r{3pt}){2-2} \cmidrule(l{3pt}r{3pt}){3-3} \cmidrule(l{3pt}r{3pt}){4-4} \cmidrule(l{3pt}r{3pt}){5-5} \cmidrule(l{3pt}r{3pt}){6-6} \cmidrule(l{3pt}r{3pt}){7-7}
& {\specialcell{(1)}} & {\specialcell{(2)}} & {\specialcell{(3)}} & {\specialcell{(4)}} & {\specialcell{(5)}} & {\specialcell{(6)}}\\
\midrule
\addlinespace[0.5em]
\multicolumn{7}{l}{\textit{Panel A: Full Sample}}\\
\midrule \hspace{1em}\specialcell{Average Depression \\ Index: friends} & 0.74*** & -0.33*** & 0.24*** & 0.15*** & 0.40*** & 0.30***\\
\hspace{1em} & (0.05) & (0.09) & (0.04) & (0.04) & (0.05) & (0.05)\\
\hspace{1em}Observations & 53,912 & 36,065 & 51,813 & 50,816 & 51,941 & 51,579\\
\addlinespace[0.5em]
\multicolumn{7}{l}{\textit{Panel B: Women}}\\
\midrule \hspace{1em}\specialcell{Average Depression \\ Index: friends} & 0.84*** & -0.41*** & 0.25*** & 0.24*** & 0.44*** & 0.28***\\
\hspace{1em} & (0.09) & (0.10) & (0.05) & (0.07) & (0.06) & (0.06)\\
\hspace{1em}Observations & 28,703 & 19,272 & 27,797 & 27,339 & 27,856 & 27,659\\
\addlinespace[0.5em]
\multicolumn{7}{l}{\textit{Panel C: Men}}\\
\midrule \hspace{1em}\specialcell{Average Depression \\ Index: friends} & 0.69*** & -0.23* & 0.25*** & 0.06 & 0.37*** & 0.34***\\
\hspace{1em} & (0.08) & (0.13) & (0.05) & (0.06) & (0.06) & (0.07)\\
\hspace{1em}Observations & 25,195 & 16,784 & 24,003 & 23,465 & 24,071 & 23,906\\
\addlinespace[0.5em]
\multicolumn{7}{l}{\textit{Panel D: Above median \# friends}}\\
\midrule \hspace{1em}\specialcell{Average Depression \\ Index: friends} & 0.89*** & -0.57*** & 0.31*** & 0.17*** & 0.42*** & 0.33***\\
\hspace{1em} & (0.08) & (0.10) & (0.05) & (0.05) & (0.05) & (0.06)\\
\hspace{1em}Observations & 34,654 & 23,653 & 33,558 & 32,948 & 33,640 & 33,400\\
\addlinespace[0.5em]
\multicolumn{7}{l}{\textit{Panel E: Below median \# friends}}\\
\midrule \hspace{1em}\specialcell{Average Depression \\ Index: friends} & 0.60*** & -0.04 & 0.16*** & 0.14** & 0.37*** & 0.27***\\
\hspace{1em} & (0.09) & (0.11) & (0.05) & (0.06) & (0.07) & (0.07)\\
\hspace{1em}Observations & 19,258 & 12,412 & 18,255 & 17,868 & 18,301 & 18,179\\
\midrule
\hspace{1em}School FE & {X} & {X} & {X} & {X} & {X} & {X}\\
\hspace{1em}Clustered Standard Errors & {by: SchoolID} & {by: SchoolID} & {by: SchoolID} & {by: SchoolID} & {by: SchoolID} & {by: SchoolID}\\
Mean Full Sample & 0.00 & 2.86 & 0.30 & 0.45 & 0.36 & 0.31\\
Mean Women Sample & 0.26 & 2.94 & 0.29 & 0.48 & 0.35 & 0.29\\
Mean Men Sample & -0.24 & 2.78 & 0.32 & 0.42 & 0.36 & 0.33\\
Mean Friends > Median & 0.11 & 2.91 & 0.30 & 0.44 & 0.38 & 0.34\\
Mean Friends $\leq$ Median & -0.11 & 2.80 & 0.30 & 0.46 & 0.32 & 0.27\\
\bottomrule
\multicolumn{7}{l}{\rule{0pt}{1em}\textit{Note: }}\\
\multicolumn{7}{l}{\rule{0pt}{1em}I include controls for sex, age, race, parental education, parental work status, parental job, and school fixed effects. }\\
\multicolumn{7}{l}{\rule{0pt}{1em}* p $<$ 0.1, ** p $<$ 0.05, *** p $<$ 0.01}\\
\end{tabular}}
\end{table}
\end{document}

Line cut in \tabularx{}

I have a code below. I would like to remove the white space before the highlighted part.
\documentclass[10pt,english,8pt]{beamer}
\usetheme{default}
\usepackage{booktabs}
\usepackage{tabularx}
%\newcommand{\iid}{\stackrel{\mathrm{iid}}{\sim}}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcolumntype{R}{>{\raggedright\arraybackslash}X}
\newcolumntype{L}{>{\raggedleft\arraybackslash}X}
\title{Beamer Template}
\author{TeXstudio Team}
\begin{document}
\begin{frame}{Frame Title}
\begin{table}[htbp]
%\begin{table}[!ht]
\centering
\scriptsize
%\caption{Title }
\label{tab:5}
\begin{tabularx}{1\textwidth}
{>{\raggedright\arraybackslash}p{2cm}
>{\centering\arraybackslash}p{1.75cm}
>{\centering\arraybackslash}p{1.75cm}
>{\centering\arraybackslash}p{1.75cm}
>{\centering\arraybackslash}p{1.75cm}
}
%\multicolumn{11}{#{}p{1\textwidth}#{}}{\footnotesize
% Caption }\\
\toprule
& Log Probability of Default & Log Recovery Rate
& Log Spread & Log Leverage \\ [0.5ex]
& (1) & (2) & (3) & (4) \\
\midrule
Log Damage
&0.017** &-0.003*** & 0.002 & -0.157 \\
&(0.0008) &(0.003) & (0.075) & (0.178) \\
Log Adaptation
&-0.157*** &0.012 &-0.463** & 0.514*** \\
&(0.021) &(0.007) &(0.182) &(0.193)\\
Log Damage X Log Adaptation
&0.006 &0.018* &-0.398 &0.535 \\
&(0.030) &(0.011) &(0.249) &(0.513)\\
Constant
&-1.227***&-0.117***&2.244***&0.101\\
&(0.026)&(0.009)&(0.187)&(0.237)\\
\midrule
Observations &926 &925 &458 &177 \\
$R^2$ &41\% &19\% &8\% &38\%\\
Adjusted $R^2$ &40\%&17\%&4\%&32\%\\
Residual Std. Error
& 0.071 & 0.025 & 0.373 & 0.264 \\
& (df = 907) & (df = 906) & (df = 439) & (df = 159)\\
F Statistic
& 35.253*** & 11.455*** & 2.104***  & 5.811***  \\
& (df = 18; 907)& (df = 18; 906)& (df = 18; 439)& (df = 17; 159)\\
\bottomrule
\addlinespace
\multicolumn{4}{#{}p{0.8\textwidth}#{}}{\footnotesize \tiny{Note: *p<0.1; **p<0.05; ***p<0.01 }} \\
\end{tabularx}
%\end{table}
\end{table}
\end{frame}
\end{document}
Log Damage X Log Adaptation is too long to fit into a single cell. The easiest fix is to manually move the second part of the phrase into the next line.
If you are using a tabularx, you should use at least one X column to make sure the table can adapt to the available space
I would use #{} in front and after the other columns to get rid of the extra space
To make reading the table easier, I would add some additional vertical space between the blocks
Are you abusing the poor X as multiplication sign? Don't!
the syntax \tiny{...} is wrong. It is a switch and does not take an argument. Use {\tiny ...} instead.
Missing math mode around the < signs
most of your table should be in math mode to get correct minus sign and proper spacing around operators, but I'm too lazy to add this in the example below...
\documentclass[10pt,english,8pt]{beamer}
\usetheme{default}
\usepackage{booktabs}
\usepackage{tabularx}
%\newcommand{\iid}{\stackrel{\mathrm{iid}}{\sim}}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcolumntype{R}{>{\raggedright\arraybackslash}X}
\newcolumntype{L}{>{\raggedleft\arraybackslash}X}
\title{Beamer Template}
\author{TeXstudio Team}
\begin{document}
\begin{frame}{Frame Title}
\begin{table}[htbp]
%\begin{table}[!ht]
\centering
\scriptsize
%\caption{Title }
\label{tab:5}
\begin{tabularx}{1\textwidth}
{
#{}
>{\raggedright\arraybackslash}X
>{\centering\arraybackslash}p{1.75cm}
>{\centering\arraybackslash}p{1.75cm}
>{\centering\arraybackslash}p{1.75cm}
>{\centering\arraybackslash}p{1.75cm}
#{}
}
%\multicolumn{11}{#{}p{1\textwidth}#{}}{\footnotesize
% Caption }\\
\toprule
& Log Probability of Default & Log Recovery Rate
& Log Spread & Log Leverage \\ [0.5ex]
& (1) & (2) & (3) & (4) \\
\midrule
Log Damage
&0.017** &-0.003*** & 0.002 & -0.157 \\
&(0.0008) &(0.003) & (0.075) & (0.178) \\\addlinespace
Log Adaptation
&-0.157*** &0.012 &-0.463** & 0.514*** \\
&(0.021) &(0.007) &(0.182) &(0.193)\\\addlinespace
Log Damage $\times$
&0.006 &0.018* &-0.398 &0.535 \\
Log Adaptation &(0.030) &(0.011) &(0.249) &(0.513)\\\addlinespace
Constant
&-1.227***&-0.117***&2.244***&0.101\\
&(0.026)&(0.009)&(0.187)&(0.237)\\
\midrule
Observations &926 &925 &458 &177 \\
$R^2$ &41\% &19\% &8\% &38\%\\
Adjusted $R^2$ &40\%&17\%&4\%&32\%\\\addlinespace
Residual Std. Error
& 0.071 & 0.025 & 0.373 & 0.264 \\
& (df = 907) & (df = 906) & (df = 439) & (df = 159)\\\addlinespace
F Statistic
& 35.253*** & 11.455*** & 2.104*** & 5.811*** \\
& (df = 18; 907)& (df = 18; 906)& (df = 18; 439)& (df = 17; 159)\\
\bottomrule
\addlinespace
\multicolumn{4}{#{}p{0.8\textwidth}#{}}{\footnotesize {\tiny Note: *$p<0.1$; **$p<0.05$; ***$p<0.01$ }} \\
\end{tabularx}
%\end{table}
\end{table}
\end{frame}
\end{document}

modelsummary unexpected latex output

With the following code I produce the laTex table in the image below. As you might notice there are a few things wrong with the output.
The title is missing
P-values in the wrong place
The footnote is misaligned
Any help is greatly appreciated!
library(tidyverse)
library(modelsummary)
library(gt)
data <- as.data.frame(ChickWeight)
mod_control <- lm(weight ~ Time , data = data)
mod_treat <- lm(weight ~ Time + Diet, data = data)
mod_one_list <- list(mod_control, mod_treat)
# coefmap
cm <- c("(Intercept)"="Konstant",
"Time" = "Tid",
"Num.Obs." = "n")
# gof_map
gm <- list(list(raw = "nobs", clean = "N", fmt = 0))
# title
tit <- "En beskrivning här"
# produce table
modelsummary(mod_one_list,
output = "gt",
stars = T,
title = tit,
coef_map = cm,
gof_map = gm,
vcov = "HC1") %>%
tab_spanner(label = '(1)', columns = 2) %>%
tab_spanner(label = "(2)", columns = 3) %>%
tab_footnote("För standardfel använder vi HC1",
locations = cells_body(rows = 1, columns = 2)) %>%
as_latex() %>%
cat()
This is an issue with the gt package. When adding both footnotes and
source notes (which is what modelsummary uses to report significance
stars), gt puts both types of notes in different mini-pages. This
breaks alignment in LaTeX.
You can see this by inspecting the code of this minimal example:
library(gt)
dat <- mtcars[1:4, 1:4]
gt(dat) |>
tab_source_note(source_note = "source note") |>
tab_footnote("footnote", locations = cells_body(rows = 1, columns = 2)) |>
as_latex() |>
cat()
## \captionsetup[table]{labelformat=empty,skip=1pt}
## \begin{longtable}{rrrr}
## \toprule
## mpg & cyl & disp & hp \\
## \midrule
## 21.0 & 6\textsuperscript{1} & 160 & 110 \\
## 21.0 & 6 & 160 & 110 \\
## 22.8 & 4 & 108 & 93 \\
## 21.4 & 6 & 258 & 110 \\
## \bottomrule
## \end{longtable}
## \vspace{-5mm}
## \begin{minipage}{\linewidth}
## \textsuperscript{1}footnote \\
## \end{minipage}
## \begin{minipage}{\linewidth}
## source note\\
## \end{minipage}
I am not sure if the gt maintainers would consider this a “bug”, but
it might be worth it to report it on their repository anyway:
https://github.com/rstudio/gt/issues
For what it’s worth, I think that the default LaTeX output with
modelsummary(model, output="latex") generally works better, because it
uses kableExtra, which seems to prioritize LaTeX a bit more.

How to align math equations even when some variables are missing?

I want the variables and signs aligned in a set of equations.
Desired output:
What I am trying in Mathjax:
\[
\begin{align}
2&x_1 - x_2 &+ 1.5&x_3 &= 8 \\
&x_1 &- 4&x_3 &= -1
\end{align}
\]
What I got:
So, what do I have to do to have the same alignment as in the "desired output" image?
There are a number of ways to achieve this. Below I use an array (with appropriate stretch and spacing), alignat and align coupled with eqparbox for measuring similarly-tagged boxes (this latter approach requires two compilations with every change in the largest element associated with every <tag>):
\documentclass{article}
\usepackage{amsmath,eqparbox}
%\usepackage{xparse}% If you have LaTeX2e < 2020-10-01
% https://tex.stackexchange.com/a/34412/5764
\makeatletter
% \eqmathbox[<tag>][<align>]{<math>}
\NewDocumentCommand{\eqmathbox}{o O{c} m}{%
\IfValueTF{#1}
{\def\eqmathbox###1##2{\eqmakebox[#1][#2]{$##1##2$}}}
{\def\eqmathbox###1##2{\eqmakebox{$##1##2$}}}
\mathpalette\eqmathbox#{#3}
}
\makeatother
\begin{document}
\[
\renewcommand{\arraystretch}{1.2}
\setlength{\arraycolsep}{0pt}
\begin{array}{ r c r c r c r }
2 x_1 & {}-{} & x_2 & {}+{} & 1.5 x_3 & {}={} & 8 \\
x_1 & & & {}-{} & 4 x_3 & {}={} & -7
\end{array}
\]
\begin{alignat*}{4}
2 x_1 & {}-{} & x_2 & {}+{} & 1.5 x_3 = {} && 8 \\
x_1 & & & {}-{} & 4 x_3 = {} && -7
\end{alignat*}
\begin{align*}
\eqmathbox[x1][r]{2 x_1} - \eqmathbox[x2][r]{x_2} + \eqmathbox[x3][r]{1.5 x_3} &= \eqmathbox[c][r]{8} \\
\eqmathbox[x1][r]{ x_1} \phantom{{}-{}} \eqmathbox[x2][r]{} - \eqmathbox[x3][r]{4 x_3} &= \eqmathbox[c][r]{-7}
\end{align*}
\end{document}
All yield similar output:

Left align block of equations

I want to left align a block of equations. The equations in the block itself are aligned, but that's not related at all to my question! I want to left align the equations rather than have them centered all the time, because it looks dumb with narrow centered equations.
Example, I want to left align this
\begin{align*}
|\vec a| &= \sqrt{3^{2}+1^{2}} = \sqrt{10} \\
|\vec b| &= \sqrt{1^{2}+23^{2}} = \sqrt{530} \\
\cos v &= \frac{26}{\sqrt{10} \cdot \sqrt{530}} \\
v &= \cos^{-1} \left(\frac{26}{\sqrt{10} \cdot \sqrt{530}}\right) \\
v &= \uuline{69.08...\degree}
\end{align*}
but also this
\begin{align*}
f(x) = -1.25x^{2} + 1.5x
\end{align*}
How is this done? If it's even possible.
Try to use the fleqn document class option.
\documentclass[fleqn]{article}
(See also http://en.wikibooks.org/wiki/LaTeX/Basics for a list of other options.)
You can use \begin{flalign}, like the example bellow:
\begin{flalign}
&f(x) = -1.25x^{2} + 1.5x&
\end{flalign}
Try this:
\begin{flalign*}
&|\vec a| = \sqrt{3^{2}+1^{2}} = \sqrt{10} & \\
&|\vec b| = \sqrt{1^{2}+23^{2}} = \sqrt{530} &\\
&\cos v = \frac{26}{\sqrt{10} \cdot \sqrt{530}} &\\
&v = \cos^{-1} \left(\frac{26}{\sqrt{10} \cdot \sqrt{530}}\right) &\\
\end{flalign*}
The & sign separates two columns, so an & at the beginning of a line means that the line starts with a blank column.
The fleqn option in the document class will apply left aligning setting in all equations of the document. You can instead use \begin{flalign}. This will align only the desired equations.

Resources