Latex - Issue on newcommand and Tabular - latex

The following code is producing error: Incomplete \if; all text was ignored after line.
I tried adding \\ on the end each if (...\eval{#1+#2}}}} \\%). But this only added a line on my table.
\begin{table}[H]
\tiny
\centering
\def\arraystretch{2}
\setlength{\tabcolsep}{5.2pt}
\caption{xxxxx}
\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\eval}{m}{\int_eval:n {#1}}
\ExplSyntaxOff
\newcommand*{\drawtable}[5]{%
\if a#5%
\multicolumn{1}{c}{\multirow{1}{*}{#1}} & \multicolumn{1}{c}{\multirow{1}{*}{#2}} & \multicolumn{1}{c}{\multirow{1}{*}{\textbf{\eval{#1+#2}}}}%
\else\if b#5%
\multicolumn{1}{c}{\multirow{1}{*}{#3}} & \multicolumn{1}{c}{\multirow{1}{*}{#4}} & \multicolumn{1}{c}{\multirow{1}{*}{\textbf{\eval{#3+#4}}}}%
\fi\fi%
}%
\begin{tabular}{ccccccccccccccccccccccccc}
\multicolumn{1}{c}{\multirow{1}{*}{}} &
\multicolumn{1}{c}{\multirow{1}{*}{}} &
\multicolumn{3}{c}{\multirow{1}{*}{\textbf{xxxxx}}} & \\ \hline
\multicolumn{1}{c}{\multirow{1}{*}{}} &
\multicolumn{1}{c}{\multirow{1}{*}{}} &
\multicolumn{2}{c}{\multirow{1}{*}{\textbf{yyy}}} & \\ \cline{3-4}
\multicolumn{1}{c}{\multirow{1}{*}{}} &
\multicolumn{1}{c}{\multirow{1}{*}{}} &
\multicolumn{1}{c}{\multirow{1}{*}{\textbf{aa}}} &
\multicolumn{1}{c}{\multirow{1}{*}{\textbf{bb}}} &
\multicolumn{1}{c}{\multirow{1}{*}{\textbf{ccc}}} & \\ \hline
\multicolumn{1}{c}{\multirow{2}{*}{\textbf{rr}}} &
\multicolumn{1}{c}{\multirow{1}{*}{\textbf{N.}}} &
\drawtable{18}{8}{4}{7}{a}
& \\
\multicolumn{1}{c}{\multirow{1}{*}{}} &
\multicolumn{1}{c}{\multirow{1}{*}{\textbf{N.}}} &
\drawtable{18}{8}{4}{7}{b}
& \\ \hline
\end{tabular}
\end{table}

Related

How to fit a Latex table to a given document size?

I tried a lot of things to fit the table to the given size of the document. Unfortunately it did not work without destroying the format.
\documentclass[10pt,a4paper,twocolumn]{article}
\usepackage[top=30pt,bottom=30pt,left=48pt,right=120pt]{geometry}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage[latin1]{inputenc}
\begin{document}
\begin{table}[!htbp] \centering
\begin{tabular} {#{\extracolsep{1pt}}lcc}
\\[-1.8ex]\hline
& \multicolumn{2}{c}{\textit{First Stage Regression}} \
\cr \cline{2-3}
\hline \\[-1.8ex]
\\[-1.8ex] & \multicolumn{1}{c}{ \% foreign born
while in college} & \multicolumn{1}{c}{\% foreign born
while in high
school} \\
\\[-1.8ex] & (1) & (2) \\
\hline \\[-1.8ex]
1960 Distribution
of Immigrants across States & 3.613e+07$^{***}$ & 1.204e+07 $^{***}$ \\
& (8.26e+04) & (2.74e+04) \\
\hline \\[-1.8ex]
Observations & 3,588,372 & 3,588,372 \\
$R^2$ & 0.051 & 0.051 \\
Adjusted $R^2$ & 0.051 & 0.051 \\
Residual Std. Error & 26.649(df = 3588371) & 8.834(df = 3588371) \\
F Statistic & 191184.297$^{***}$ (df = 1.0; 3588371.0) & 193328.460$^{***}$ (df = 1.0; 3588371.0) \\
\hline
\hline \\[-1.8ex]
\textit{Note:} & \multicolumn{2}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\
\end{tabular}
\end{table}
\end{document}
Does anyone have an idea? Thanks
I suggest to use a table* environment so that your table will span over both columns.
You can safe some room by making very long cells into multi-line cells. The tabularray package makes this easy.
Are you 100% sure that your document is really encoded in latin1? That sounds very usual this millennium...
You should load the hyperref package after the other packages.
\documentclass[10pt,a4paper,twocolumn]{article}
\usepackage[top=30pt,bottom=30pt,left=48pt,right=120pt]{geometry}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage[utf8]{inputenc}
\usepackage{tabularray}
\usepackage{hyperref}
\begin{document}
\begin{table*}
\begin{tblr}{
colspec={X[l]X[c]X[c]},
}
\hline
& \SetCell[c=2]{} \textit{First Stage Regression} & \\
\cline{2-3}
& \% foreign born while in college (1) & \% foreign born while in high school (2)\\
\hline
\SetCell[r=2]{} 1960 Distribution of Immigrants across States & 3.613e+07$^{***}$ & 1.204e+07$^{***}$ \\
& (8.26e+04) & (2.74e+04) \\
\hline
Observations & 3,588,372 & 3,588,372 \\
$R^2$ & 0.051 & 0.051 \\
Adjusted $R^2$ & 0.051 & 0.051 \\
Residual Std. Error & 26.649(df = 3588371) & 8.834(df = 3588371) \\
F Statistic & 191184.297$^{***}$ (df = 1.0; 3588371.0) & 193328.460$^{***}$ (df = 1.0; 3588371.0) \\
\hline
\hline
\textit{Note:} & \SetCell[c=2]{r} $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01 \\
\end{tblr}
\end{table*}
\end{document}
Just add the \resizebox parameter before tabular and don't forget to add the brackets before and after the tabular. You can put it in different ways, such as \resizebox{\textwidth}{!}, \resizebox{0.5\textwidth}{!}, \resizebox{3cm}{!}, ...
\documentclass[10pt,a4paper,twocolumn]{article}
\usepackage[top=30pt,bottom=30pt,left=48pt,right=120pt]{geometry}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage[latin1]{inputenc}
\begin{document}
\begin{table}[!htbp] \centering
\resizebox{\textwidth}{!}{
\begin{tabular} {#{\extracolsep{1pt}}lcc}
\\[-1.8ex]\hline
& \multicolumn{2}{c}{\textit{First Stage Regression}} \
\cr \cline{2-3}
\hline \\[-1.8ex]
\\[-1.8ex] & \multicolumn{1}{c}{ \% foreign born
while in college} & \multicolumn{1}{c}{\% foreign born
while in high
school} \\
\\[-1.8ex] & (1) & (2) \\
\hline \\[-1.8ex]
1960 Distribution
of Immigrants across States & 3.613e+07$^{***}$ & 1.204e+07 $^{***}$ \\
& (8.26e+04) & (2.74e+04) \\
\hline \\[-1.8ex]
Observations & 3,588,372 & 3,588,372 \\
$R^2$ & 0.051 & 0.051 \\
Adjusted $R^2$ & 0.051 & 0.051 \\
Residual Std. Error & 26.649(df = 3588371) & 8.834(df = 3588371) \\
F Statistic & 191184.297$^{***}$ (df = 1.0; 3588371.0) & 193328.460$^{***}$ (df = 1.0; 3588371.0) \\
\hline
\hline \\[-1.8ex]
\textit{Note:} & \multicolumn{2}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\
\end{tabular}
}
\end{table}
\end{document}

Overfull \hbox and Package array Errors. Latex tabular

I am getting trouble to fit my table - tabular to the paragraph width.
This is the error:
Overfull \hbox (341.06795pt too wide) in paragraph at lines 391--411
Package array Error: Illegal pream-token (M): `c' used.
Package array Error: Illegal pream-token (4cm): `c' used
I have been trying one thousand things and nothing seems to work.
I am using this template from Overleaf, where you can find the document class.
This is my code.
\documentclass{JASSS}
\usepackage{amsmath,systeme}
\usepackage{tabularx,colortbl}
\usepackage{booktabs}
\title{XXXXX}
\reviewcopy{true}
\author[1]{}
\affil[1]{}
\email{XX}
\usepackage{natbib}
\setcitestyle{authoryear,round,aysep={}}
\begin{document}
\maketitle
\begin{abstract}
Bla bla
\end{abstract}
\section{xxxx}
\begin{table}[h!]
\centering
\begin{tabular}{ |M{4cm}||M{4cm}|M{4cm}|M{4cm}|}
% \begin{tabular}{|c|c|c|c|}
\cline{2-4}
\multicolumn{1}{c|}{} & \multicolumn{3}{c|}{Initial value parameter combinations ($t_0=1932$)} \\
\hline
Scenarios from 2020 & $\phi_{B}=\phi_{C}=0.05$ and 1932 electoral results & $\phi_{B}=0.05, \phi_{C}=0.055$ and 1932 electoral results & $\phi_{B}=\phi_{C}=0.05$ and electoral tie \\
\hline
$\phi_{D}=\phi_{E}=0.01$, $\gamma_{B}=\gamma_{C}=\gamma_{D}=\gamma_{E}=0.01$ & S0000 & S0100 & S1000 \\
\hline
\rowcolor{blue!20}
$\phi_{D}=0.015,\phi_{E}=0.01$, $\gamma_{B}=\gamma_{C}=\gamma_{D}=\gamma_{E}=0.01$ & S0001 & S0101 & S1001 \\
\hline
\rowcolor{red!20}
$\phi_{D}=0.01,\phi_{E}=0.015$, $\gamma_{B}=\gamma_{C}=\gamma_{D}=\gamma_{E}=0.01$ & S0010 & S0110 & S1010 \\
\hline
\rowcolor{yellow!20}
$\phi_{D}=0.01,\phi_{E}=0.01$, $\gamma_{B}=\gamma_{C}=\gamma_{D}=\gamma_{E}=0.015$ & S0011 & S0111 & S1011 \\
\hline
\end{tabular}
\bibliographystyle{jasss}
\bibliography{references}
\end{document}
The M column type is not defined by default. If you want to use it, you have to somehow define it. I don't know what you indent it to do, but here some dummy code:
\documentclass{JASSS}
\usepackage{array}
\usepackage{amsmath,systeme}
\usepackage{tabularx,colortbl}
\usepackage{booktabs}
\newcolumntype{M}[1]{p{#1}}
\begin{document}
\begin{table}[h!]
\centering
\begin{tabular}{ |M{4cm}||M{4cm}|M{4cm}|M{4cm}|}
% \begin{tabular}{|c|c|c|c|}
\cline{2-4}
\multicolumn{1}{c|}{} & \multicolumn{3}{c|}{Initial value parameter combinations ($t_0=1932$)} \\
\hline
Scenarios from 2020 & $\phi_{B}=\phi_{C}=0.05$ and 1932 electoral results & $\phi_{B}=0.05, \phi_{C}=0.055$ and 1932 electoral results & $\phi_{B}=\phi_{C}=0.05$ and electoral tie \\
\hline
$\phi_{D}=\phi_{E}=0.01$, $\gamma_{B}=\gamma_{C}=\gamma_{D}=\gamma_{E}=0.01$ & S0000 & S0100 & S1000 \\
\hline
\rowcolor{blue!20}
$\phi_{D}=0.015,\phi_{E}=0.01$, $\gamma_{B}=\gamma_{C}=\gamma_{D}=\gamma_{E}=0.01$ & S0001 & S0101 & S1001 \\
\hline
\rowcolor{red!20}
$\phi_{D}=0.01,\phi_{E}=0.015$, $\gamma_{B}=\gamma_{C}=\gamma_{D}=\gamma_{E}=0.01$ & S0010 & S0110 & S1010 \\
\hline
\rowcolor{yellow!20}
$\phi_{D}=0.01,\phi_{E}=0.01$, $\gamma_{B}=\gamma_{C}=\gamma_{D}=\gamma_{E}=0.015$ & S0011 & S0111 & S1011 \\
\hline
\end{tabular}
\end{table}
\end{document}

Having problems fitting a table to a Column in LaTeX

I have my template set that subsections go into 2 columns, however when I try and insert a table, it does not compile instead being left blank. I think the reason is that the table is too wide/not being fitted.
Here is my LaTeX
\subsection{On examination}
\begin{table}[]
\begin{tabular}{|l|l|l|}
\hline
{\ul \textbf{Symptom}} & {\ul \textbf{Patient}} & {\ul \textbf{Normal}} \\ \hline
\textit{Temperature} & 36.7C & 36-36.8 \\ \hline
\textit{BMI} & 31 kg/m\textasciicircum{}2 & 18.5-24.9 \\ \hline
\textit{Heart Rate} & 70bpm & 60-100bpm \\ \hline
\textit{Blood Pressure} & 145/80 mmHg & \textless{}140/90mmHg \\ \hline
\textit{Respiratory Rate} & 12 breaths/min & 12-18 breaths/min \\ \hline
\textit{Misc} & & \\ \hline
\end{tabular}
\end{table}

how to draw this table

I'm trying to draw the table of the image, but I can not find the way to do it with latex.
\begin{tabular}{|C{1cm}|C{1cm}|C{1cm}|C{1cm}|C{1cm}|C{1cm}|C{1cm}}
\hline
Tipo RA & \multicolumn{2}{c|}{Area [m$^2$]}& \multicolumn{2}{c|}{Volumen Semiesferico truncado} \\
1 &&&&&&\\ \hline
2 &&&&&&\\ \hline
3 &&&&&&\\ \hline
4 &&&&&&\\ \hline
5 &&&&&&\\ \hline
6 &&&&&&\\ \hline
7 &&&&&&\\ \hline
\end{tabular}
I'd suggest using booktabs:
\documentclass{article}
\usepackage{booktabs,siunitx,makecell}
\newcommand{\pz}{\phantom{0}}
\sisetup{
group-minimum-digits = 3
}
\begin{document}
\noindent
\begin{tabular}{ l *{6}{c} }
\toprule
& \multicolumn{4}{c}{Area [$m^2$]} & \\
\cmidrule{2-5}
& \multicolumn{2}{c}{Circular} & \multicolumn{2}{c}{El\'{\i}ptica} &
\multicolumn{2}{c}{\smash{\makecell[cb]{Volumen Semiesf\'erico \\ truncado [$m^3$]}}} \\
\cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(lr){6-7}
Tipo RA & Rango & Promedio & Rango & Promedio & Rango & Promedio \\
\midrule
ALTA & \num{20866}--\num{28983} & \num{24446} & \num{13354}--\num{18549} & \num{15645} & \num{589840}--\num{1201636} & \num{883786} \\
MEDIA & \num{16634}--\num{27254} & \num{20241} & \num{12476}--\num{20440} & \num{15180} & \num{539605}--\num{1131633} & \num{730582} \\
BAJA & \pz\num{3822}--\num{25577} & \num{16097} & \pz\num{3407}--\num{22735} & \num{14307} & \pz\num{75664}--\num{1063784} & \num{647366} \\
\bottomrule
\end{tabular}
\end{document}
In general, there is no need for vertical rules in a tabular, as the columnar display of information supports horizontal separation naturally.
Creating tables directly in LaTeX is not an easy task, I suggest you use tools that allow you to create tables for latex online such as: https://www.tablesgenerator.com/
Please add the following required packages to your document preamble:
\usepackage{multirow,multicol, makecell, booktabs}
\begin{tabular}{*{7}{c}}
\toprule
& \multicolumn{4}{c}{\smash{\makecell[c]{Area [$m^2$]}}}
& \multicolumn{2}{c}{\smash{\makecell[c]{Volumen Semiesférico \\ truncado [$m^3$]}}}\\
\cline{2-5}
& \multicolumn{2}{c}{Circular} & \multicolumn{2}{c}{Eliptica} & \\
\cline{2-7}
Tipo RA & Rango & Promedio & Rango & Promedio & Rango & Promedio \\
\midrule
ALTA & 20866-28983 & 24464 & 20866-28983 & 24464 & 20866-28983 & 24464 \\
MEDIA & 20866-28983 & 24464 & 20866-28983 & 24464 & 20866-28983 & 24464 \\
BAJA & 20866-28983 & 24464 & 20866-28983 & 24464 & 20866-28983 & 24464 \\
\bottomrule
\end{tabular}
Just a no-booktabs sketch, to reproduce your table as similar as possible. Using booktabs is of course recommended for a readability concept that holds everywhere but the first sight on the table would then change very much.
\documentclass{article}
\usepackage{multirow}
\begin{document}
Text
\begin{table}[ht]
{\sffamily%
\begin{tabular}{*{7}{c|}}
% be careful when nesting \multicolumn and \multirow
\cline{2-7}
\multirow{2}{*}{ } & \multicolumn{4}{|c|}{Area [$m^2$]} & \multicolumn{2}{c|}{\multirow{2}{*}{\shortstack[c]{Volumen Semiesf\'erico\\ truncado [$m^3$]}}}\\
\cline{2-5}
& \multicolumn{2}{c|}{Circular} & \multicolumn{2}{c|}{El\'{\i}ptica} & \multicolumn{1}{c}{} & \\
% second part below is standard
\hline
\multicolumn{1}{|c|}{Tipo RA} & Rango & Promedio & Rango & Promedio & Rango & Promedio\\
\hline
\multicolumn{1}{|c|}{ALTA} & 2 - 2 & 3 & 4 - 4 & 5 & 6 - 6 & 7\\
\hline
\multicolumn{1}{|c|}{MEDIA} & 2 - 2 & 3 & 4 - 4 & 5 & 6 - 6 & 7\\
\hline
\multicolumn{1}{|c|}{BAJA} & 2 - 2 & 3 & 4 - 4 & 5 & 6 - 6 & 7\\
\hline
\end{tabular}
}%
\end{table}
Text.
\end{document}
The output of this code is:
Alignments, line breaks and spacing may be considerably improved, also depending on your time and objectives.

How can I format long division of polynomials in MathJax?

I try to format my math like this in MathJax:
How can I do long division of polynomials in MathJax?
Old post, but I couldn't find an answer elsewhere, so here's how I solved this issue with two side-by-side arrays. Couldn't figure out how to make the lines shorter as I could in LaTex, but this was good enough for me:
<span class="math display">\[\begin{array}{r}
\\
x^2 - 3) \\
\phantom{.} \\
\phantom{.} \\
\phantom{.} \\
\phantom{.} \\
\phantom{.} \\
\phantom{.} \\
\phantom{.} \\
\phantom{.} \\
\phantom{.} \\
\end{array}
\begin{array}{rrrrrr}
& & x^3 & -2x^2 & +4x & -6 \\ \hline
x^5 & -2x^4 & + x^3 & &-8x & +18 \\
-x^5 & &+3x^3 \\ \hline
& -2x^4 & +4x^3 \\
& 2x^4 & &-6x^2 \\ \hline
& & 4x^3 & -6x^2 & -8x \\
& & -4x^3 & &+12x \\ \hline \\
& & &-6x^2 & +4x & +18 \\
& & & 6x^2 & & -18 \\ \hline
& & & & 4x
\end{array}\]</span>

Resources