Import table from another latex file - latex

Is it possible to have a tables.tex file and then import specific tables into other files?

Yes it is!
Copy the following example code into a file called main.tex (or any other name):
\documentclass{article}
\begin{document}
Some text.
\input{tables.tex}
Some other text.
\end{document}
Then copy the following code for one table into a file called tables.tex:
\begin{table}[ht]
\centering
\begin{tabular}{|l|c|r|}
\hline
a & b & c\\
\hline
d & e & f\\
\hline
\end{tabular}
\end{table}
Save them both in the same folder and compile the first one.
If your tables.tex file is in a subfolder or different folder, edit the path in the code accordingly like \input{subfolder/tables.tex} or \input{../other_folder/tables.tex}.
If you need two or more different tables in different sections of the same document, then you need to input more files, e.g. table1.tex, table2.tex, ecc.
Also very important: input command vs. include command.

Related

How to I import and display my .csv file in a Beamer presentation?

I have a csv file called csv1.csv that looks like:
name|surname|grade
Maier|Hans,|A
Huber|Anna|B
Weißbäck|Werner|C
(So my csv is a table with 4 rows and 3 columns. There are tabular lines and not "|" in my csv file - like a spreadsheet.)
My actual file is a .csv file in my desktop that I imported in my overleaf. I have no idea why I am unable to simply display the table, let alone format it, using the csvsimple package. What I tried:
\documentclass[10pt]{beamer}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{csvsimple}
\begin{document}
\begin{table}[h]
\centering
\csvautobooktabular{csv1.csv}
\end{table}
\caption{My File}
\end{document}
I just want to display my csv file in my beamer and format it a bit (capitalize headers, make them bold, etc) instead of pasting a screenshot of it. If there is any other package that can help me, please feel free to suggest! Thank you!
By default csvsimple assumes commas as the column separator; but your csv1.csv uses pipe characters. So you'll need to specify separator=pipe.
The \caption should also go before the \end{table}. So this should work:
\documentclass[10pt]{beamer}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{csvsimple}
\begin{document}
\begin{frame}
\begin{table}
\csvautobooktabular[separator=pipe]{csv1.csv}
\caption{My File}
\end{table}
\end{frame}
\end{document}
(Demo on Overleaf: https://www.overleaf.com/read/tnbwbwfbjpqg)
Adapting the example of the following link, you can get the following result
This is the code:
\documentclass{beamer}
\usepackage{csvsimple}
% file content grade.csv
% name,givenname,matriculation,gender,grade
% Maier,Hans,12345,m,1.0
% Huber,Anna,23456,f,2.3
% Weisbaeck,Werner,34567,m,5.0
\begin{document}
\begin{frame}{Title frame}
\begin{table}
\caption{Caption of table}
\begin{tabular}{l|c}%
\hline
\bfseries Person & \bfseries Matr.~No.% specify table head
\csvreader[head to column names]{grade.csv}{}% use head of csv as column names
{\\\hline\givenname\ \name & \matriculation}% specify your columns here
\end{tabular}
\end{table}
\end{frame}
\end{document}
Keep in mind that the *.CSV file is in the same work board and the data is separated by a ","

Table caption misplaced and no list of tables generated

I am writing an APA-style document in RMarkdown using Papaya package, which compiles into LaTeX, and then to pdf, by pandoc. I know a small amout of LaTeX, and I am not familiar with RMarkdown, and was asked to use it. So I have two problems here, and thank you in advance for any help. The problems might be related to Rmarkdown/Papaya, but they could also be purely LaTeX related, so even if you don't know about RMarkdown, you may still know something.
The first problem. I have the following table in my RMarkdown file,
\begin{center}
\begin{tabular}{| c | c | c | c | c | c | c |}
\hline
Model & Composability & Unboundness & Comm. & Sync. & Distribution & Mobility\\ \hline
$\pi$ & explicit & explicit & shared-mem & sync & NA & NA \\ \hline
Join & explicit & implicit & shared-mem & sync & implicit & implicit\\ \hline
Ambient & explicit & explicit & shared-mem & sync & explicit & explicit\\ \hline
Actor & NA & implicit & message-pass & async & implicit & implicit \\ \hline
\end{tabular}
\captionof{table}{Summary of Properties of Concurrency Models}\label{summary}
\end{center}
I was wondering how I can make the caption one-line and centered in the generated pdf file? It now spans two lines and is not centered (see this screenshot).
The second problem. I also would like to generate a list of tables after tables of contents. When I add \listoftables to the beginning part of the RMarkdown file together with other LaTeX command, \listoftables is missing from the generated .tex file, but the other commands shown below such as \newcommand are preserved in the .tex file.
```{r analysis-preferences}
# Seed for random number generation
set.seed(42)
knitr::opts_chunk$set(cache.extra = knitr::rand_seed)
```
\listoftables
\newcommand{\defeq}{\vcentcolon=}
\newcounter{equationset}
\newcommand{\equationset}[1]{% \equationset{<caption>}
\refstepcounter{equationset}% Step counter
\noindent\makebox[\linewidth]{Equation set~\theequationset: #1}}
\lstset{
basicstyle=\ttfamily,
mathescape
}
Then I notice there is an option at the beginning of the RMarkdown file
tablelist : no
and I change it to
tablelist : yes
but I get the following new compilation warning:
Warning message:
Package tocloft Warning: \#starttoc has already been redefined; tocloft bailing
out. on input line 1147.
The LaTeX packages that I included in the RMarkdown file are:
header-includes:
- \usepackage{amsmath}
- \usepackage{mathtools}
- \usepackage{listings}
- \usepackage{caption}

How to implement full LaTeX syntax in Rmarkdown?

I have a two part question regarding the use of LaTeX in Rmarkdown:
1) When working in Rmarkdown (with the intent to render to PDF), is there a rule for when we should just use the double dollar signs ($$) to write something in LaTeX or when we should use the LaTeX syntax to begin and end all of our LaTeX code (e.g. \documentclass{...}, \begin{document}, \end{document}, etc.
I believe I have read that it is okay to just use the latter option, and Rmarkdown will ignore all of the escaped latex commands if the document is rendered to anything other than PDF.
2) The reason I am asking, in this case, is that I am trying to incorporate some labelled matrix multiplication in an Rmarkdown document I am writing. Specifically, I would like to include some matrices that take the form show on this page. Here is the code:
\documentclass{article}
\usepackage{amsmath}
\newenvironment{spmatrix}[1]
{\def\mysubscript{#1}\mathop\bgroup\begin{pmatrix}}
{\end{pmatrix}\egroup_{\textstyle\mathstrut\mysubscript}}
\begin{document}
\begin{equation}
\begin{spmatrix}{A}
a & b \\
c & d
\end{spmatrix}
\begin{spmatrix}{x}
x_1 \\
x_2
\end{spmatrix}
=
\begin{spmatrix}{b}
b_1 \\
b_2
\end{spmatrix}
\end{equation}
\end{document}
How would one implement this code in Rmarkdown? Do you need to move the \usepackage call into the YAML as suggested in other threads discussing the loading of LaTeX packages in Rmarkdown? Is the first line, \documentclass{article} even needed within an Rmarkdown document?
I'm new to all of this, and thusfar, I've been able to get by using the double dollar signs to set off all my LaTeX code for simple equations and even a simple matrix here and there that I've tried to write, but for mathematical expressions that require more formatting, aligning, multi-line proofs, etc., most of the examples I've encountered are on the TEX boards written with syntax similar to the code above. I haven't been able to figure out how to implement these types of examples in Rmarkdown. Any helpful suggestions or pointers where to better understand this issue would be much appreciated!
It is indeed possible to put most body-level LaTeX constructs into the body of your Rmd file. Other backends will ignore these constructs, but the result might look change. So from my point of view your are binding yourself to PDF output. But that might be fine in your case.
Concerning your concrete problem:
amsmath is already included by the default template, which also takes care of \ḑocumentclass and the document environment.
You need to add the environment definition into a separate tex file (in my case preamble.tex) and include that file via the YAML headers.
You can then use the LaTeX constructs as is in the Rmd body.
Putting things together:
---
output:
pdf_document:
keep_tex: yes
includes:
in_header: preamble.tex
---
\begin{equation}
\begin{spmatrix}{A}
a & b \\
c & d
\end{spmatrix}
\begin{spmatrix}{x}
x_1 \\
x_2
\end{spmatrix}
=
\begin{spmatrix}{b}
b_1 \\
b_2
\end{spmatrix}
\end{equation}

Environment inside a longtable with LaTeX

I would like to create a new environment to print a header and a footer between sections of a table.
I did this:
\documentclass{article}
\usepackage{longtable}
\newenvironment{env}{Heading&&& \\}{\hline \\}
\begin{document}
\begin{longtable}{p{7cm}lrr}
\begin{env}
Content&b&c&d
\end{env}
\end{longtable}
\end{document}
but I get insulted by the compiler. See here for the complete output.
Does someone see the problem?
There are two problems here. First, you need an \\ at the end of the "Content&b&c&d" line. Second, environments don't work inside tabular/longtable — that's where most of your error messages are coming from. It may be possible to diddle them into working, but it's way beyond my TeX-fu. This is the best I can come up with:
\documentclass{article}
\usepackage{longtable}
\newcommand{\startenv}{Heading\tabularnewline}
\newcommand{\stopenv}{\hline\tabularnewline}
\begin{document}
\begin{longtable}{p{7cm}lrr}
\startenv
Content&b&c&d \\
\stopenv
\end{longtable}
(It is not strictly necessary to use \tabularnewline instead of \\, but it will avoid headaches if you ever mix this with other environments that use \\ for their own purposes.)

Storing and recalling variable number of text strings in LaTeX

I'm currently writing an API document in LaTeX. I have a tabular environment with a list of error codes and descriptions, like so:
\begin{tabular}{|c|l|}
\hline
\textbf{Code} & \textbf{Description} \\ \hline
1 & (description of error 1) \\ \hline
2 & (description of error 2) \\ \hline
\end{tabular}
At various places later in the document, I reference an error's code and its description together, like so:
Possible error conditions:
\begin{itemize}
\item 1---(description of error 1)
\end{itemize}
I want to automate this process so I don't have to retype the descriptions every time. I have tried using a counter, labels, and the \savebox command, but it's pretty cumbersome:
\newcounter{error}
% Inside the tabular environment:
\newsavebox{\ErrorOne}
\savebox{\ErrorOne}{(description of error 1)}
\refstepcounter{error} \label{ErrorOne} \arabic{error} & \usebox{\ErrorOne} \\ \hline
and later, to reference the error,
\ref{ErrorOne}---\usebox{\ErrorOne}
I particularly object to having to use ErrorOne for the labels but \ErrorOne (with the leading backslash) for the saveboxes. I also don't really want names like ErrorOne, since I might need to change the order at some point. What I want is to be able to define a few commands:
\newerror{errorlabel}{Description} % defines the error (doesn't output anything)
\errorcode{errorlabel} % outputs the error code
\errordesc{errorlabel} % outputs the error description
and then be able to say something like
\newerror{ArgumentError}{Too many arguments}
\newerror{DatabaseError}{Could not connect to database}
% Inside the tabular environment:
\errorcode{ArgumentError} & \errordesc{ArgumentError} \\ \hline
\errorcode{DatabaseError} & \errordesc{DatabaseError} \\ \hline
% Later on:
\errorcode{DatabaseError}---\errordesc{DatabaseError}
with the error codes (1, 2, 3, ...) being automatically generated like labels would be.
Any ideas?
The following works for me
\catcode`\#=11
\newcounter{error}
\def\newerror#1#2{\refstepcounter{error}%
\expandafter\xdef\csname errno##1\endcsname{\arabic{error}}%
\expandafter\xdef\csname errds##1\endcsname{#2}%
}
\def\errorcode#1{\expandafter\printerrinfo \csname errno##1\endcsname}
\def\errordesc#1{\expandafter\printerrinfo \csname errds##1\endcsname}
\def\printerrinfo#1{\ifx#1\relax\errmessage{Error code is invalid}%
\else\expandafter#1\fi}
\catcode`\#=12
\newerror{ArgumentError}{Too many arguments}
\newerror{DatabaseError}{Could not connect to database}
\errorcode{DatabaseError}---\errordesc{DatabaseError}
In your preamble, create new commands for each error, then just call the command:
\newcommand{\errorone}{this is the error and its description}
then in the body, just call the new command:
\begin{tabular}{|c|l|}
\hline
\textbf{Code} & \textbf{Description} \\ \hline
1 & \errorone \\ \hline

Resources