Hi I am using the following code to try and dynamically set the number of columns in a Latex tabular environment:
\documentclass{article}
\usepackage{array}
\newcounter{numcols}
\setcounter{numcols}{4}
\begin{document}
\begin{tabular}{
$\loop
\stepcounter{col}
\ifnum \value{col} < \value{numcols}
c &
\else
c
\fi
\ifnum \value{col} = \value{numcols}
\setcounter{col}{0}
\repeat$
}
\hline
\loop
\stepcounter{col}
\ifnum \value{col} < \value{numcols}
\textbf{Column \thecol} &
\else
\textbf{Column \thecol} \\
\fi
\ifnum \value{col} = \value{numcols}
\setcounter{col}{0}
\repeat
\hline
\loop
\stepcounter{row}
\ifnum \value{row} < 10
\loop
\stepcounter{col}
\ifnum \value{col} < \value{numcols}
Cell \therow,\thecol &
\else
Cell \therow,\thecol \\
\fi
\ifnum \value{col} = \value{numcols}
\setcounter{col}{0}
\repeat
\repeat
\hline
\end{tabular}
\end{document}
However, it throws an error that Illegal pream-token ($): c' used if I remove the $ I receive the same error but it says Illegal pream-token (\loop): c' used.
Any help on solving this error would be much appreciated.
I need to create a table with a dynamic number of columns in Latex
Related
I am writing a paper in APS journal template : revtex4-2, when I import my table I have this error :
Missing control sequence inserted. \end{tabulary}. But this table has no problem in other template for example IEEE. I dont know how fix it. I really appreciate any help.
\documentclass[aps,pra,twocolumn,floatfix,footinbib,notitlepage,superscriptaddress,groupaddress,showpacs]{revtex4-2}
\usepackage{graphicx,graphics,times,bm,bbm,bbold,amssymb,amsmath,amsfonts,dsfont,hyperref,mathrsfs,color,caption,subcaption,cancel}
\usepackage{adjustbox}
\usepackage{tabularx}
\usepackage{tabulary}
\usepackage{graphicx}% Include figure files
\usepackage{dcolumn}% Align table columns on decimal point
\usepackage{bm}
\begin{document}
\preprint{AIP/123-QED}
%\title[Sample title]{Sample Title:\\with Forced Linebreak\footnote{Error!}}% Force line breaks with \\
\title[]
\author{}
\affiliation{}
\author{}
\affiliation{}
\email{}
\date{\today}
\begin{abstract}
\end{abstract}
% \keywords{}
% \maketitle
\section{Introduction}\label{sec:introduction}
\section{Theoretical Analysis}
\begin{table}[h!]
\centering
\caption{List of Parameters Values }
\label{table:1}
\begin{adjustbox}{max width=8.5 cm}
\begin{tabulary}{\columnwidth}{#{}llr#{}}
\toprule
\textbf{Parameters} & \textbf{Values} & \textbf{Units}\\
\midrule
$\lambda$ & 1550 & $nm$ \\
\bottomrule
\end{tabulary}
\end{adjustbox}
\end{table}.
\end{document}
%
% ****** End of file aipsamp.tex ******
I suggest the new and much more flexible tabularray package instead.
Also don't scale content which contains text, this will result in an suboptimal result. If in your real document, you must make the table smaller, use a smaller font size instead of scaling it with adjustbox.
(you should really clean up your preamble and remove duplicates and outdated packages)
\documentclass[aps,pra,twocolumn,floatfix,footinbib,notitlepage,superscriptaddress,groupaddress,showpacs]{revtex4-2}
\usepackage{graphicx,graphics,times,bm,bbm,bbold,amssymb,amsmath,amsfonts,dsfont,hyperref,mathrsfs,color,caption,subcaption,cancel}
\usepackage{adjustbox}
\usepackage{tabularx}
\usepackage{tabulary}
\usepackage{graphicx}% Include figure files
\usepackage{dcolumn}% Align table columns on decimal point
\usepackage{bm}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\begin{document}
\preprint{AIP/123-QED}
%\title[Sample title]{Sample Title:\\with Forced Linebreak\footnote{Error!}}% Force line breaks with \\
\title[]
\author{}
\affiliation{}
\author{}
\affiliation{}
\email{}
\date{\today}
\begin{abstract}
\end{abstract}
% \keywords{}
% \maketitle
\section{Introduction}\label{sec:introduction}
\section{Theoretical Analysis}
\begin{table}[h!]
\centering
\caption{List of Parameters Values }
\label{table:1}
% \begin{adjustbox}{max width=8.5 cm}
\begin{tblr}{width=0.8\linewidth,colspec={#{}X[l]X[l]X[r]#{}}}
\toprule
\textbf{Parameters} & \textbf{Values} & \textbf{Units}\\
\midrule
$\lambda$ & 1550 & $nm$ \\
\bottomrule
\end{tblr}
% \end{adjustbox}
\end{table}
\end{document}
%
% ****** End of file aipsamp.tex ******
I am trying to make a letter head to make a certificate to put in a thesis. I have posted the MWE below.
There are 2 issues I need help with
The cell alignment of the letterhead, is not proper. I am not sure how to correct it.
I need the double lines to be in red colour, I wasn't able to get it.
The final output should be something like this image
enter image description here
\documentclass[12 pt,a4 paper]{article}
\usepackage[top=1cm,left=1in,right=1 in,bottom=1in]{geometry}
\usepackage{times}
\usepackage{graphicx}
\usepackage{marvosym}
\usepackage[onehalfspacing]{setspace}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{fancyhdr}
\usepackage{xcolor}
\pagestyle{fancy}
\usepackage{multirow}
\fancyhf{}
\noindent
\makebox[0pt][l]{\rule[.7ex]{\linewidth}{#1}}%
\rule[.3ex]{\linewidth}{#1}}
\begin{document}
\pagestyle{empty}
\vspace{12 mm}
\begin{tabular}{c c c}
\multicolumn{1}{r}{{\Large Bangalore}} & \multirow{1}{*}{\includegraphics[width=0.175\linewidth]{BUB_Emblem}} & \multicolumn{1}{l}{{\Large University}}\\
%\multicolumn{1}{r}{\Huge{Bangalore}} & \multirow{1}{*}{\includegraphics[width=0.15\linewidth]{BUB_Emblem}} & \multicolumn{1}{l}{\Huge{University}}\\
\large{Dr Vijaykumar H Doddamani} & & \multicolumn{1}{l}{Department of Physics} \\
\multicolumn{1}{r}{M.Sc., M. Phil., PhD} && \multicolumn{1}{l}{Ph. Off: 080-22961471/84} \\
&&\multicolumn{1}{l}{Fax: 080-23219295}\\
\multicolumn{1}{l}{\textbf{\large Professor}} & & \multicolumn{1}{l}{Mobile : 9481300346} \\
\multicolumn{1}{l}{Email: drvkd#gmail.com} & Jnanabharathi Campus & \multicolumn{1}{l}{Mobile : 9448673274} \\
\multicolumn{1}{r}{profvijaykumarhd#bub.ernet.in} & Bengaluru - 560 056 & \multicolumn{1}{l}{WhatsApp :9741716744} \\
%
\end{tabular}
\vskip2mm
\vspace{2mm}
\doublerule[1pt]
\end{document}
With this code, This is the output I am getting.
enter image description here
I have put in the arrows and comments in this imagearrows and comments for width alinment
\documentclass[12 pt,a4 paper]{article}
\usepackage[top=1cm,left=1in,right=1 in,bottom=1in]{geometry}
\usepackage[table]{xcolor}
\usepackage{times}
\usepackage{graphicx}
\usepackage{marvosym}
\usepackage[onehalfspacing]{setspace}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{fancyhdr}
\pagestyle{fancy}
\usepackage{multirow}
\fancyhf{}
\newcommand{\doublerule}[1][.4pt]{%
\noindent%
\makebox[0pt][l]{\rule[.7ex]{\linewidth}{#1}}%
\rule[.3ex]{\linewidth}{#1}}
\begin{document}
\pagestyle{empty}
\vspace{12 mm}
\centerline{%
\makebox[\paperwidth]{%
\begin{tabular}{#{\hspace{.08\paperwidth}} p{.32\paperwidth} p{.2\paperwidth} p{.32\paperwidth} #{\hspace{.08\paperwidth}} }
\multicolumn{1}{#{}r#{}}{\Large Bangalore} & \centering\multirow{1}{*}{\includegraphics[width=.9\linewidth]{example-image-duck}} & \Large University\\
\multicolumn{1}{#{}r#{}}{\large Dr Vijaykumar H Doddamani} & & Department of Physics\\
\multicolumn{1}{#{}r#{}}{M.Sc., M. Phil., PhD} && Ph. Off: 080-22961471/84 \\
&&Fax: 080-23219295\\
\textbf{\large Professor} & & Mobile : 9481300346 \\
Email: drvkd#gmail.com & \centering Jnanabharathi Campus & Mobile : 9448673274 \\
\hphantom{Email:} profvijaykumarhd#bub.ernet.in & \centering Bengaluru - 560 056 & WhatsApp :9741716744 \\[2mm]
\arrayrulecolor{red}\hline\hline
\end{tabular}%
}}
\end{document}
I am trying to compile the following beamer presentation in rmarkdown. But when it tries to render rmarkdown document an error occurs
! LaTeX Error: Option clash for package xcolor.
I searched Internet and found out that I need to include \PassOptionsToPackage{table}{xcolor} before the line \documentclass. Applying options to already loaded package and also see here
How do I do it in RMarkdown?
---
title: "My report"
output:
beamer_presentation:
keep_tex: true
header-includes:
- \usepackage{booktabs}
- \usepackage{tabu}
- \usepackage{threeparttable}
- \usepackage{threeparttablex}
- \usepackage[normalem]{ulem}
- \usepackage[table]{xcolor}
---
```{r setup, include=FALSE}
library(knitr)
library(kableExtra)
knitr::opts_chunk$set(echo = FALSE)
```
```{r positioned_table}
dt <- mtcars[1:5, 1:6]
kable(dt) %>%
kable_styling("striped", full_width = F) %>%
column_spec(5:7, bold = T) %>%
row_spec(3:5, bold = T, color = "white", background = "#D7261E")
```
The resulting .tex file is
\PassOptionsToPackage{unicode=true}{hyperref} % options for packages loaded elsewhere
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[ignorenonframetext,]{beamer}
\usepackage{pgfpages}
\setbeamertemplate{caption}[numbered]
\setbeamertemplate{caption label separator}{: }
\setbeamercolor{caption name}{fg=normal text.fg}
\beamertemplatenavigationsymbolsempty
% Prevent slide breaks in the middle of a paragraph:
\widowpenalties 1 10000
\raggedbottom
\setbeamertemplate{part page}{
\centering
\begin{beamercolorbox}[sep=16pt,center]{part title}
\usebeamerfont{part title}\insertpart\par
\end{beamercolorbox}
}
\setbeamertemplate{section page}{
\centering
\begin{beamercolorbox}[sep=12pt,center]{part title}
\usebeamerfont{section title}\insertsection\par
\end{beamercolorbox}
}
\setbeamertemplate{subsection page}{
\centering
\begin{beamercolorbox}[sep=8pt,center]{part title}
\usebeamerfont{subsection title}\insertsubsection\par
\end{beamercolorbox}
}
\AtBeginPart{
\frame{\partpage}
}
\AtBeginSection{
\ifbibliography
\else
\frame{\sectionpage}
\fi
}
\AtBeginSubsection{
\frame{\subsectionpage}
}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{textcomp} % provides euro and other symbols
\else % if luatex or xelatex
\usepackage{unicode-math}
\defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
\fi
% use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
% use microtype if available
\IfFileExists{microtype.sty}{%
\usepackage[]{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\IfFileExists{parskip.sty}{%
\usepackage{parskip}
}{% else
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
}
\usepackage{hyperref}
\hypersetup{
pdftitle={My report},
pdfborder={0 0 0},
breaklinks=true}
\urlstyle{same} % don't use monospace font for urls
\newif\ifbibliography
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{0}
% set default figure placement to htbp
\makeatletter
\def\fps#figure{htbp}
\makeatother
\usepackage{booktabs}
\usepackage{tabu}
\usepackage{threeparttable}
\usepackage{threeparttablex}
\usepackage[normalem]{ulem}
\usepackage[table]{xcolor}
\title{My report}
\date{}
\begin{document}
\frame{\titlepage}
\begin{frame}
\begin{table}[H]
\centering
\begin{tabular}{l|r|r|r|>{\bfseries}r|>{\bfseries}r|>{\bfseries}r}
\hline
& mpg & cyl & disp & hp & drat & wt\\
\hline
Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
\hline
Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
\hline
\rowcolor[HTML]{D7261E} \textcolor{white}{\textbf{Datsun 710}} & \textcolor{white}{\textbf{22.8}} & \textcolor{white}{\textbf{4}} & \textcolor{white}{\textbf{108}} & \textcolor{white}{\textbf{93}} & \textcolor{white}{\textbf{3.85}} & \textcolor{white}{\textbf{2.320}}\\
\hline
\rowcolor[HTML]{D7261E} \textcolor{white}{\textbf{Hornet 4 Drive}} & \textcolor{white}{\textbf{21.4}} & \textcolor{white}{\textbf{6}} & \textcolor{white}{\textbf{258}} & \textcolor{white}{\textbf{110}} & \textcolor{white}{\textbf{3.08}} & \textcolor{white}{\textbf{3.215}}\\
\hline
\rowcolor[HTML]{D7261E} \textcolor{white}{\textbf{Hornet Sportabout}} & \textcolor{white}{\textbf{18.7}} & \textcolor{white}{\textbf{8}} & \textcolor{white}{\textbf{360}} & \textcolor{white}{\textbf{175}} & \textcolor{white}{\textbf{3.15}} & \textcolor{white}{\textbf{3.440}}\\
\hline
\end{tabular}
\end{table}
\end{frame}
\end{document}
Using very usefull comment from #samcarter, I found the exact answer. All I need is the following yaml header in my rmarkdown document:
---
title: "My report"
output:
beamer_presentation:
keep_tex: true
classoption: xcolor=table
header-includes:
- \usepackage{booktabs}
- \usepackage{tabu}
- \usepackage{threeparttable}
- \usepackage{threeparttablex}
- \usepackage[normalem]{ulem}
---
To use the table xcolor option with beamer on can pass this to the documentclass. In traditional latex
\documentclass[xcolor={table}]{beamer}
For markdown https://bookdown.org/yihui/rmarkdown/pdf-document.html#latex-options suggests that on can use
classoption: xcolor={table}
\begin{equation}
\tau _{2}(r_{0})=\left\{\begin{matrix}
\frac{R^{d}-r_{c}^{d}}{D_{1}}\frac{r_{0}^{-1}-a^{-1}}{-3} \\
\frac{R^{d}-r_{c}^{d}}{D_{1}}\frac{r_{c}^{-1}-a^{-1}}{-3}
\end{matrix}\right.
\end{equation}
Basically I am wrtting an equation in Latex which should be displayed in the form
But Latex reports error "Missing } inserted" on the third line. What is the problem? Thanks a lot!
The cases environment from the amsmath package exists for exactly this purpose:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{equation}
\tau_{2}(r_{0}) =
\begin{cases}
\frac{R^{d}-r_{c}^{d}}{D_{1}}\frac{r_{0}^{-1}-a^{-1}}{-3} \\
\frac{R^{d}-r_{c}^{d}}{D_{1}}\frac{r_{c}^{-1}-a^{-1}}{-3}
\end{cases}
\end{equation}
\end{document}
This also makes it easy to add description, e.g.
\begin{equation}
\tau_{2}(r_{0}) =
\begin{cases}
\frac{R^{d}-r_{c}^{d}}{D_{1}}\frac{r_{0}^{-1}-a^{-1}}{-3} & \text{if } c = 0\\
\frac{R^{d}-r_{c}^{d}}{D_{1}}\frac{r_{c}^{-1}-a^{-1}}{-3} & \text{otherwise} \\
\end{cases}
\end{equation}
I made some changes to the equation and this works. Not sure if it is what you were trying to achieve
\documentclass{article}
\usepackage{graphicx}
\usepackage{amsmath}
\begin{document}
\title{Introduction to \LaTeX{}}
\author{Author's Name}
\maketitle
\[
\tau _{2}(r_{0})= \left[
\begin{array}{ccc}
\dfrac{R^{d}-r_{c}^{d}}{D_{1}} & \dfrac{r_{0}^{-1}-a^{-1}}{-3}\\
\dfrac{R^{d}-r_{c}^{d}}{D_{1}} & \dfrac{r_{c}^{-1}-a^{-1}}{-3}
\end{array}
\right]
\]
\end{document}
Pandoc is unable to convert LaTeX tables with an "% END" comment:
\documentclass{article}
\begin{document}
\begin{table}[h]
\centering
\begin{tabular}{|l|l|l|}
a & a & a \\
a & a & a \\
a & a & a \\
% END
\end{tabular}
\caption{example}
\label{tab:example}
\end{table}
\end{document}
The same example without the comment works fine.