Aligning tags in a table [Latex] [closed] - latex

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 using the following code to create a table in Latex:
\begin{table}[]
\centering
\renewcommand{\arraystretch}{1}
\caption{Example table.}
\label{tab:model-testbed}
\renewcommand{\arraystretch}{1.1}
\resizebox{\linewidth}{!}{%
\begin{tabular}{|c|l|l|}
\hline
Comment & \multicolumn{1}{c|}{Device1} & \multicolumn{1}{c|}{Device2} \\ \hline
\begin{tabular}[c]{#{}c#{}}BlaBla \\ Sth \end{tabular} &
\begin{tabular}[c]{#{}l#{}}
\begin{equation}
\tag{1}\label{t3:1}
\begin{aligned}
y(x) = 66.1 + 1.11x - 1.28 \times 10^{-8}x^2 \\ +15.08 \times 10^{-2}x^1
\end{aligned}
\end{equation}
\end{tabular} &
\begin{tabular}[c]{#{}l#{}}
\begin{equation}
\tag{2}\label{t3:2}
\begin{aligned}
y(x) =20.33 + 1.24y - 1.74 \times 10^{-1}x^5 \\ + 1.14 \times 10^{-7}y^5
\end{aligned}
\end{equation}
\end{tabular} \\ \hline
\begin{tabular}[c]{#{}c#{}}BlaBla \\ Sth\end{tabular} &
\begin{equation}
\tag{3}\label{t3:3}
\begin{aligned}
w(x) =2.13 + 0.03x
\end{aligned}
\end{equation}
&
\begin{equation}
\tag{4} \label{t3:4}
\begin{aligned}
er(x) =4.85 + 33.56y
\end{aligned}
\end{equation}
\\ \hline
\begin{tabular}[c]{#{}c#{}}BlaBla\\ Sth\end{tabular} &
\begin{equation}
\tag{5}\label{t3:5}
\begin{aligned}
b(x) =12.2 + 5.20y
\end{aligned}
\end{equation}
&
\begin{equation}
\tag{6}\label{t3:6}
\begin{aligned}
a(x) =1.11 + 1.11x - 2.543 \times 10^{-2}x^2
\end{aligned}
\end{equation}
\hline
\end{tabular}}
\end{table}
And it outputs the following table:
I am struggling to find a way how to align the tags. As you can see next to each formula there is a tag (1), (3), etc. , and it is positioned right next to the equation. But, I would like all the tags to be aligned. I tried with \hspace{1cm} to try to move them to the left, hoping that I can position them to the end of the column and with it align them, but it did not work.
Does anyone have an idea how I could do it?

You would need to wrap "display" equations inside minipages. The tags would correctly be pushed to the right. However, equations get centred w.r.t. surrounding environment.
One way to achieve left alignment with right tags is to add equations in-line and then use \tag{} separately. However, the latter is not allowed outside equations. It can be sorted out by manually setting the #currentlabel for very next \label{} usage; I have made a macro for that--its optional star removes parentheses in case you don't want them.
A couple remarks. I added geometry to increase the page width for demonstration because this table gets quite wide. Sometimes, environments like equations cause issues with the standard vertical spacing and this needs a manual adjustment. There are other table-related packages that sort it out, e.g. tabularray but here I have used a strut: \xmathstrut{factor} from mathtools. Also, for multi lined texts, I'd suggest to use \makecell from makecell--a very convenient macro that allows line breaks. EDIT. the vertical spacing could also be sorted by increasing so-called "gape" inside makecells--I'll leave it for you.
Here's the example
\documentclass{article}
\usepackage[margin=2cm]{geometry}
\usepackage{array}
\usepackage{makecell}
\usepackage{mathtools}
\makeatletter
\NewDocumentCommand\refanchor{sm}{
% #1 star removes partheses
% #2 reference label
\IfBooleanF{#1}{(}#2\IfBooleanF{#1}{)}
\protected#edef\#currentlabel{#2}}
\makeatother
\begin{document}
References: eq.~\ref{t3:1} on page \pageref{t3:1}.
References with parentheses: eq.~\eqref{t3:3} on page \pageref{t3:3}.
\begin{table}[tbh]
\renewcommand{\arraystretch}{1.5}
\small
\centering
\caption{Example table.}\label{tab:model-testbed}
\begin{tabular}{|c|l#{}|l#{}|}
\hline
Comment
& \multicolumn{1}{c|}{Device1}
& \multicolumn{1}{c|}{Device2} \\
\hline
\makecell{BlaBla\\Sth}
&
\begin{minipage}{0.4\linewidth}
\(\xmathstrut{1.25}
\begin{aligned}
y(x) = 66.1 + 1.11x - 1.28 \times 10^{-8}x^2 \\
+15.08 \times 10^{-2}x^1
\end{aligned}\)%
\hfill
\refanchor{1}\label{t3:1}
\end{minipage}
&
\begin{minipage}{0.4\linewidth}
\(
\begin{aligned}
y(x) =20.33 + 1.24y - 1.74 \times 10^{-1}x^5\\
+1.14 \times 10^{-7}y^5
\end{aligned}\)
\hfill
\refanchor{2}\label{t3:2}
\end{minipage} \\
\hline
\makecell{BlaBla\\Sth}
&
\begin{minipage}{0.4\linewidth}
\(\xmathstrut{1.15} w(x) =2.13 + 0.03x \)%
\hfill
\refanchor{3}\label{t3:3}
\end{minipage}
&
\begin{minipage}{0.4\linewidth}
\(er(x) = 4.85 + 33.56y\)%
\hfill
\refanchor*{4}\label{t3:4}
\end{minipage}
\\ \hline
\end{tabular}
\end{table}
\end{document}

Related

Overleaf Appendix Table Numbering

I am using overleaf to write a journal article and want to move a lot of my results table to the Appendix. So I write the following commands:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{outlines}
\usepackage{dirtytalk}
\usepackage{latexsym}
\usepackage{tabularx}
\usepackage{amsmath}
\usepackage{booktabs,caption}
\usepackage{longtable}
\usepackage{appendix}
\usepackage[para,online,flushleft]{threeparttable}
\usepackage[affil-it]{authblk}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage[a4paper, total={6in, 8in}]{geometry}
\renewcommand{\baselinestretch}{1.5}
\begin{document}
\bibliographystyle{IEEEtran}
\bibliography{bibliography.bib}
\appendix
\section{Tables}
\begin{table}[!]
\begin{scriptsize}
\centering
\caption{Reduced Form Estimates using Linear Probability Model}
\label{tab:my-table}
\begin{tabular}{#{}lcccc#{}}
\hline
\hline
& (1) & (2) & (3) & (4) \\
VARIABLES & A & B & C & D \\
\hline
&&&& \\
&&&& \\
&&&& \\
\hline
\multicolumn{5}{c}{\textit{Note:}Robust standard errors in parentheses *** p$<$0.01, ** p$<$0.05, * p$<$0.1 }
\end{tabular}
\end{scriptsize}
\end{table}
\end{document}
While the section is recognized as appendix and tables, the numbering of the tables continue from the main text. How do I go from for example: Table 12 to Table A1?
Thanks
You could use
\setcounter{table}{0}
\renewcommand{\thetable}{A\arabic{table}}
to change the numbering style of the appendix tables
Please also note that scriptsize is NOT an environment, it is a macro.
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{outlines}
\usepackage{dirtytalk}
\usepackage{latexsym}
\usepackage{tabularx}
\usepackage{amsmath}
\usepackage{booktabs,caption}
\usepackage{longtable}
\usepackage{appendix}
\usepackage[para,online,flushleft]{threeparttable}
\usepackage[affil-it]{authblk}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage[a4paper, total={6in, 8in}]{geometry}
\renewcommand{\baselinestretch}{1.5}
\begin{document}
\bibliographystyle{IEEEtran}
\bibliography{bibliography.bib}
\begin{table}[htbp]
%\begin{scriptsize}
\scriptsize
\centering
\caption{Reduced Form Estimates using Linear Probability Model}
\label{tab:my-table-}
\begin{tabular}{#{}lcccc#{}}
\hline
\hline
& (1) & (2) & (3) & (4) \\
VARIABLES & A & B & C & D \\
\hline
&&&& \\
&&&& \\
&&&& \\
\hline
\multicolumn{5}{c}{\textit{Note:}Robust standard errors in parentheses *** p$<$0.01, ** p$<$0.05, * p$<$0.1 }
\end{tabular}
%\end{scriptsize}
\end{table}
\appendix
\setcounter{table}{0}
\renewcommand{\thetable}{A\arabic{table}}
\section{Tables}
\begin{table}[htbp]
%\begin{scriptsize}
\scriptsize
\centering
\caption{Reduced Form Estimates using Linear Probability Model}
\label{tab:my-table}
\begin{tabular}{#{}lcccc#{}}
\hline
\hline
& (1) & (2) & (3) & (4) \\
VARIABLES & A & B & C & D \\
\hline
&&&& \\
&&&& \\
&&&& \\
\hline
\multicolumn{5}{c}{\textit{Note:}Robust standard errors in parentheses *** p$<$0.01, ** p$<$0.05, * p$<$0.1 }
\end{tabular}
%\end{scriptsize}
\end{table}
\end{document}

Formatting table in latex

I would like to ask how to format table above in latex - table is pretty decent but I would like to have "model" bit right not at the very end, also I would like to have no space at the very right of the table.
\documentclass{article}
\usepackage[utf8]{inputenc}
\title{table in stack}
\author{petr102030 Hrobar}
\date{November 2019}
\begin{document}
\maketitle
\section{Introduction}
\begin{table}[!htbp] \centering
\label{exp_rmse}
\begin{tabular}{#{\extracolsep{5pt}}lccccccc}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
Model & \multicolumn{1}{c}{RMSE} & \multicolumn{1}{c}{MAE} & \multicolumn{1}{c}{MAPE} & \\
\hline \\[-1.8ex]
Holt-Winters (add.) & 1256.361 & 924.962 & 3.518 &\\
Holt-Winters (mul.) & 1238.799 & 909.395 & 3.457 &\\
Parab. Trend (model 4) & 2228.310 & 1792.471 & 7.959 &\\
Lin.Trend (model 5) & 3074.307 & 2545.426 & 11.339 &\\
\hline \\[-1.8ex]
\end{tabular}
\end{table}
\end{document}
The "Model" is at the leftmost boarder of your table because the default padding is shallowed by #{\extracolsep{5pt}}. If you remove this, you'll get the padding back. (However the usual advice is to remove this padding, so please consider carefully if you really want to do this)
only specify as many columns as you have in your table and not 8 like in your example
with the siunitx package you can align the numbers nicely by their decimal markers
as already mentioned by Picaud Vincent the booktabs package is very useful to create nice looking tables. Amongst other things, it improves the vertical spacing around rules
using \label{} only makes sense if there also is a caption it can reference
I suggest to remove all the unnecessary \multicolumn{1}{...}, they don't do anything useful and can mess up the formatting
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{siunitx}
\title{table in stack}
\author{petr102030 Hrobar}
\date{November 2019}
\begin{document}
\maketitle
\section{Introduction}
\begin{table}[!htbp]
\centering
\begin{tabular}{#{}lS[table-format=4.3]S[table-format=4.3]S[table-format=2.3]}
\toprule
Model & {RMSE} & {MAE} & {MAPE} \\
\midrule
Holt-Winters (add.) & 1256.361 & 924.962 & 3.518 \\
Holt-Winters (mul.) & 1238.799 & 909.395 & 3.457 \\
Parab. Trend (model 4) & 2228.310 & 1792.471 & 7.959 \\
Lin.Trend (model 5) & 3074.307 & 2545.426 & 11.339 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}

Latex Long Equations Alignment in single column

I need multiple equations aligned in single column of a two columned document. I have tried a mixture of both align and multiline but it does not work.
\documentclass[12pt,journal,compsoc]{IEEEtran}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\begin{document}
\maketitle
\section{Introduction}
\begin{align*}
p(W_{i},T_{n})&=\begin{multline*}
p(W_{i},T_{n})\\+(p(W_{i},T_{n})*k/100)
\end{multline*}\\
p(W_{i},T_{n})&=\begin{multline*}
p(W_{i},T_{n})\\+(p(W_{i},T_{n})*k/100)
\end{multline*}\\
\end{align*}
\end{document}
It gives me the error
Package amsmath Error :\begin{multline*} allowed only in paragraph mode
There's no need for multline here; a regular align will do:
\documentclass[journal,compsoc]{IEEEtran}
\usepackage{amsmath,lipsum}
\begin{document}
\section{Introduction}
\lipsum*[1]
\begin{align*}
p(W_i, T_n) ={}& p(W_i, T_n) \\
& + (p(W_i, T_n) \times k / 100) \\
p(W_i, T_n) ={}& p(W_i, T_n) \\
& + (p(W_i, T_n) \times k / 100)
\end{align*}
\lipsum*[2]
\end{document}
Since you're aligning with a line-break, it's easier to use ={}& as opposed to the traditional &= (which would require additional \phantoms to ensure proper alignment).

How to center a vector in latex

I am using latex for my report. I am stuck in centering a vector. This is my latex code:
\begin{equation}
\centering
X_{i}=
$$\begin{bmatrix}
C_1\\
C_2\\
C_3\\
\vdots\\
\vdots\\
\vdots\\
\vdots\\
C_n\\
OlValue\\
\end{bmatrix}$$
\label{Vect}
\end{equation}
The centering command doesn't work. I tried several things but the vector is not on the center.
You're doing it wrong...
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\noindent
X \dotfill X% To show the horizontal text width/margins
\begin{equation}
X_i =
\begin{bmatrix}
C_1 \\ C_2 \\
\vdots \\
C_n
\end{bmatrix}
\end{equation}
\end{document}
Don't use \centering inside an equation. Also don't use $$...$$. The equation environment automatically centres its contents, period.

rowcolors fill whole row and not just the table row

Ok so i wrote this simple code (shown below) to create an alternate colour table using \rowcolors, yet instead of just colouring the rows of the table it colours the whole row (even more than textwidth). Any help how to fix this?
begin{table}[ht]
\scriptsize
\begin{center}
\rowcolors{1}{lightgray}{white}
\caption{...}
\begin{tabular}{p{0.45\textwidth} | p{0.55\textwidth}}
Filename & Contents \\
\hline
\hline
A & B \\
C & F \\
\end{tabular}
\end{center}
\end{table}
While 0.45\textwidth + 0.55\textwidth would seem to fit within \textwidth, each of your p-columns have an extra column separation... on both sides. To that end, you should remove them in order to make it fit within the text block boundary:
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{lipsum}
\begin{document}
\begin{table}[ht]
\scriptsize\centering
\rowcolors{1}{lightgray}{white}
\caption{This is a table.}
\begin{tabular}{
p{\dimexpr0.45\textwidth-2\tabcolsep} |
p{\dimexpr0.55\textwidth-2\tabcolsep}}
Filename & Contents \\
\hline
\hline
A & B \\
C & F
\end{tabular}
\end{table}
\lipsum[1]
\end{document}
Also, don't use the center environment; use \centering instead.

Resources