LaTeX - tabular start at new line - latex

I want to start the tabular at start of line, but when I put it directly after e.g. \paragraph{Foo}, than It will be displayed at right side of 'Foo'.
\documentclass[twocolumn,a4,titlepage]{article}
\begin{document}
\section{foo}
\paragraph{A}
\begin{tabular}{ccc}
X & Y & Z \\
X & Y & Z \\
X & Y & Z \\
\end{tabular}
\paragraph{B}
\begin{tabular}{ccc}
X & Y & Z \\
X & Y & Z \\
X & Y & Z \\
\end{tabular}
\end{document}
I know, I could put it between \begin{center} and \end{center}, but than it would float around (i.e. will sometimes be displayed after the following text) and will be centered, which looks strange for small tables.
I strongly believe that this question is already answered elsewhere, but I couldn't find it.
Thanks!!

You can force the table to start in a new line like this:
\documentclass[twocolumn,a4,titlepage]{article}
\begin{document}
\section{foo}
\paragraph{A}
\begin{tabular}{ccc}
X & Y & Z \\
X & Y & Z \\
X & Y & Z \\
\end{tabular}
\paragraph{B}\mbox{}\par
\noindent\begin{tabular}{#{}ccc}
X & Y & Z \\
X & Y & Z \\
X & Y & Z \\
\end{tabular}
\end{document}

Related

How to align piecewise functions

I thought a properly aligned piecewise function should be something like this:
Commas and character xs are perfectly aligned in two lines.
I tried
\vert x \vert =
\begin{cases}
x, &x \geq 0 \\
-x, &x < 0 \\
\end{cases}
which only aligned the domain parts like this:
In that way, the commas come after x and -x is not aligned, which looks strange.
I tried to solve that problem by adding spaces preceding the x in first line, which is:
\vert x \vert =
\begin{cases}
\enspace \; x, &x \geq 0 \\
-x, &x < 0 \\
\end{cases}
It gives the correct result (as above) but this solution seems rather strange, if the second line changes, then the first line have also to be modified to keep the spaces fit. I reckon there must be some better way to do it.
The default alignment behaviour of cases is left-left for the value and domain components. In this specific case (for aesthetic reasons), you can insert a \phantom negation to align the values:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
\lvert x \rvert = \begin{cases}
x, & x \geq 0 \\
-x, & x < 0
\end{cases}
\]
\[
\lvert x \rvert = \begin{cases}
\phantom{-}x, & x \geq 0 \\
-x, & x < 0
\end{cases}
\]
\end{document}

Changing dimensions (height and width ) of latex table generated tabularray

I need to make the LaTeX table generated here smaller in height either by changing the total height or each row height, whichever looks better. How can I do that?
Here is the code:
\begin{table}[h!]
\centering
\caption[Transformation table]{Transformations generated by Mach et al. Y in a dark cell: highly recommended, Y in a light cell: usable, N in a dark cell: unusable \label{tab:power}}
\begin{tblr}{
colspec={lXXXXX},
vlines,
hlines,
cells={halign=c},
column{1-2}={halign=l},
vspan=even,
cell{1}{2}={halign=c,bg=gray,fg=white},
cell{2}{1}={bg=gray,fg=white},
cell{3}{5-6}={bg=lightgray,font=\bfseries},
cell{5}{4-6}={bg=lightgray,font=\bfseries},
cell{6}{3}={bg=lightgray,font=\bfseries},
}
& \SetCell[c=5]{} Type of Distribution & & & &\\
\SetCell[r=5]{} \rotatebox{90}{\parbox{3.5cm}{\centering Type of Transformation}} & & Lognormal & Exponential & Gamma & Weibull \\
& Box-Cox & Y & Y & Y & Y \\
& Exponential & Y & Y & Y & Y\\
& Simple power & Y & Y & Y & Y \\
& Logarithmic & Y & Y & N & N \\
\end{tblr}
\end{table}
The extra height comes from the \parbox. If you reduce the width of the \parbox a bit, the rows become less tall.
If you need the rows to be even narrower, you can change the rowsep (but you'll have to ditch the idea of rotated text then).
\documentclass[11pt]{article}
\usepackage{tabularray}
\usepackage{geometry}
\usepackage{xcolor}
\usepackage{graphicx}
\begin{document}
\begin{table}[h!]
\centering
\caption[Transformation table]{Transformations generated by Mach et al. Y in a dark cell: highly recommended, Y in a light cell: usable, N in a dark cell: unusable \label{tab:power}}
\begin{tblr}{
colspec={lXXXXX},
vlines,
hlines,
cells={halign=c},
column{1-2}={halign=l},
vspan=even,
cell{1}{2}={halign=c,bg=gray,fg=white},
cell{2}{1}={bg=gray,fg=white},
cell{3}{5-6}={bg=lightgray,font=\bfseries},
cell{5}{4-6}={bg=lightgray,font=\bfseries},
cell{6}{3}={bg=lightgray,font=\bfseries},
% rowsep = 0mm
}
& \SetCell[c=5]{} Type of Distribution & & & &\\
\SetCell[r=5]{} \rotatebox{90}{\parbox{2.6cm}{\centering Type of Transformation}} & & Lognormal & Exponential & Gamma & Weibull \\
& Box-Cox & Y & Y & Y & Y \\
& Exponential & Y & Y & Y & Y\\
& Simple power & Y & Y & Y & Y \\
& Logarithmic & Y & Y & N & N \\
\end{tblr}
\end{table}
\end{document}

Changing latex table text direction?

I want to change the table generated by the following code so that the first column looks like the figure below by 1- changing the text direction in the first column. I made some changes regarding this post but still have a problem.
Desired output:
Current output:
\begin{table}[h!]
\centering
\caption[Transformation table]{Transformations generated by Mach et al. Y in a dark cell: highly recommended, Y in a light cell: usable, N in a dark cell: unusable \label{tab:power}}
\begin{tblr}{
colspec={XXXXXX},
vlines,
hlines,
cells={halign=c},
column{1-2}={halign=l},
hspan=even,
}
& \SetCell[c=5]{halign=c,bg=gray,fg=white} Type of Distribution & & & &\\
\SetCell[r=5]{bg=gray,fg=white} \parbox[t]{2mm}{\multirow{5}{*}{\rotatebox[origin=c]{90}{Type of \\ Transformation}}} & & Lognormal & Exponential & Gamma & Weibull \\
& Box-Cox & Y & Y & \SetCell{bg=lightgray,font=\bfseries} Y & \SetCell{bg=lightgray,font=\bfseries} Y \\
& Exponential & Y & Y & Y & Y\\
& Simple power & Y & \SetCell{bg=lightgray,font=\bfseries} Y & \SetCell{bg=lightgray,font=\bfseries} Y & \SetCell{bg=lightgray,font=\bfseries} Y \\
& Logarithmic & \SetCell{bg=lightgray,font=\bfseries} Y & Y & \SetCell{font=\bfseries} N & \SetCell{font=\bfseries} N \\
\end{tblr}
\end{table}
\begin{table}[H]
\centering
\begin{tabular}{|c|l|r|r|r|r|}
\hline
& \multicolumn{1}{|c|}{Text} & \multicolumn{1}{|c|}{Text} & \multicolumn{1}{|c|}{Text} & \multicolumn{1}{|c|}{Text} & \multicolumn{1}{|c|}{text}\\
\hline
\rotatebox{90}{\parbox{2mm}{\multirow{3}{*}{rota}}} & text &&&&\\
& text &&&&\\
& text &&&&\\
\hline
\end{tabular}
\end{table}
Use the \rotatebox before the \parbox (and don't use \multirow in a tblr, that's no longer defined there):
---
title: "misc"
author: "Me"
date: "`r Sys.Date()`"
output:
pdf_document:
keep_tex: true
extra_dependencies: caption
number_sections: yes
fig_caption: yes
classoption: table
header-includes:
- \usepackage{tabularray}
language:
label:
fig: !expr function(x) sprintf("**Figure %s.** ", x)
fontsize: 11pt
urlcolor: blue
---
\begin{table}[h!]
\centering
\caption[Transformation table]{Transformations generated by Mach et al. Y in a dark cell: highly recommended, Y in a light cell: usable, N in a dark cell: unusable \label{tab:power}}
\begin{tblr}{
colspec={lXXXXX},
vlines,
hlines,
cells={halign=c},
column{1-2}={halign=l},
vspan=even,
cell{1}{2}={halign=c,bg=gray,fg=white},
cell{2}{1}={bg=gray,fg=white},
cell{3}{5-6}={bg=lightgray,font=\bfseries},
cell{5}{4-6}={bg=lightgray,font=\bfseries},
cell{6}{3}={bg=lightgray,font=\bfseries},
}
& \SetCell[c=5]{} Type of Distribution & & & &\\
\SetCell[r=5]{} \rotatebox{90}{\parbox{3.5cm}{\centering Type of Transformation}} & & Lognormal & Exponential & Gamma & Weibull \\
& Box-Cox & Y & Y & Y & Y \\
& Exponential & Y & Y & Y & Y\\
& Simple power & Y & Y & Y & Y \\
& Logarithmic & Y & Y & N & N \\
\end{tblr}
\end{table}

Alternative to Bordermatrix for MathJax

I'm using bordermatrix to write matrix with coefficient on the top and on the left:
I would like to display a similar matrix on my website where I use Mathjax but I get a [Math Processing Error]. Do you have a MathJax-alternative to bordermatrix to get the same result?
You can use the following construction:
\begin{align}
f(x) &= ax^2 + bx + c \\
&= \begin{array}{c c}
& \begin{array} {#{} c c c #{}}
u_1 & \cdots & u_q
\end{array} \\
\begin{array}{c}
e_1 \\ \vdots \\ e_n
\end{array}\hspace{-1em} &
\left(
\begin{array}{#{} c c c #{}}
u_{11} & \cdots & u_{1q} \\
\vdots & & \vdots \\
u_{n1} & \cdots & u_{nq}
\end{array}
\right) \\
\mbox{} % Blank line to match column names so as to align the = vertically
\end{array} \\[-12pt] % Correction for blank line
&= ax^2 + bx + c
\end{align}
Depending on whether you're aligning it with other content, you might not need the vertical adjustment I inserted.
Tested on Math.SE.

Right margin in table cells aligned to the right in Latex

I have the following table in Latex
Created with this code:
\begin{table}[h]
\caption{aaaa}
\label{tab:treatments}
\centering
\scalebox{0.8}{
\begin{tabular}{l c c c}
\toprule
\tabhead{Por 1} & \tabhead{Por 3} & \tabhead{Quantificazione log-dim} & \tabhead{cintin\'a} \\
\midrule
A & M & 99.3\%& 5.4\\
B& A & 2.0\%& 4.6\\
C & N & 5.8\% & 4.6\\
D & N & 3.5\% & 4.26\\
E & K & 22.5\% & 3.7\\
\bottomrule\\
\end{tabular}
}
\end{table}
I would like to have the third column of percentages aligned to the right
At the same time the column label is too large. If I aligned the column to the right it would and up visually weird.
In a Word table I would align the column to the right and than add a right margin to the cells. The numbers would move toward the cell center while keeping their alignment to the right.
How do I do that in Latex? How do I add a margin to the right?
There are a number of options you can try:
Original:
Multi-row header:
Manual alignment of centred columns using \phantoms:
Manual alignment of centred columns using \eqmakeboxes (from eqparbox):
Automated alignment of centred columns using siunitx:
\documentclass{article}
\usepackage{booktabs,graphicx,makecell,siunitx,eqparbox}
\newcommand{\tabhead}{\textbf}
\begin{document}
Original:
\begin{tabular}{ l c c c }
\toprule
\tabhead{Por 1} & \tabhead{Por 3} & \tabhead{Quantificazione log-dim} & \tabhead{cintin\'a} \\
\midrule
A & M & 99.3\% & 5.4 \\
B & A & 2.0\% & 4.6 \\
C & N & 5.8\% & 4.6 \\
D & N & 3.5\% & 4.26 \\
E & K & 22.5\% & 3.7 \\
\bottomrule
\end{tabular}
\bigskip
Multi-row header:
\begin{tabular}{ l c c c }
\toprule
\tabhead{Por 1} & \tabhead{Por 3} & \tabhead{\makecell[b]{Quantificazione \\ log-dim}} & \tabhead{cintin\'a} \\
\midrule
A & M & 99.3\% & 5.4 \\
B & A & 2.0\% & 4.6 \\
C & N & 5.8\% & 4.6 \\
D & N & 3.5\% & 4.26 \\
E & K & 22.5\% & 3.7 \\
\bottomrule
\end{tabular}
\bigskip
Manual alignment of centred columns using \verb|\phantom|s:
\begin{tabular}{ l c c c }
\toprule
\tabhead{Por 1} & \tabhead{Por 3} & \tabhead{\makecell[b]{Quantificazione \\ log-dim}} & \tabhead{cintin\'a} \\
\midrule
A & M & 99.3\% & 5.4\phantom{0} \\
B & A & \phantom{0}2.0\% & 4.6\phantom{0} \\
C & N & \phantom{0}5.8\% & 4.6\phantom{0} \\
D & N & \phantom{0}3.5\% & 4.26 \\
E & K & 22.5\% & 3.7\phantom{0} \\
\bottomrule
\end{tabular}
\bigskip
Manual alignment of centred columns using \verb|\eqmakebox|es:
\begin{tabular}{ l c c c }
\toprule
\tabhead{Por 1} & \tabhead{Por 3} & \tabhead{\makecell[b]{Quantificazione \\ log-dim}} & \tabhead{cintin\'a} \\
\midrule
A & M & \eqmakebox[log][r]{99.3\%} & \eqmakebox[cint][l]{5.4} \\
B & A & \eqmakebox[log][r]{ 2.0\%} & \eqmakebox[cint][l]{4.6} \\
C & N & \eqmakebox[log][r]{ 5.8\%} & \eqmakebox[cint][l]{4.6} \\
D & N & \eqmakebox[log][r]{ 3.5\%} & \eqmakebox[cint][l]{4.26} \\
E & K & \eqmakebox[log][r]{22.5\%} & \eqmakebox[cint][l]{3.7} \\
\bottomrule
\end{tabular}
\newpage
Automated alignment of centred columns using \verb|siunitx|:
\begin{tabular}{ l c S[table-format = 2.1, table-space-text-post = \%] S[table-format = 1.2] }
\toprule
\tabhead{Por 1} & \tabhead{Por 3} & \tabhead{\makecell[b]{Quantificazione \\ log-dim}} & \tabhead{cintin\'a} \\
\midrule
A & M & 99.3\% & 5.4 \\
B & A & 2.0\% & 4.6 \\
C & N & 5.8\% & 4.6 \\
D & N & 3.5\% & 4.26 \\
E & K & 22.5\% & 3.7 \\
\bottomrule
\end{tabular}
\end{document}

Resources