Skip to content

Instantly share code, notes, and snippets.

@tnaia
Created June 23, 2014 07:53
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tnaia/16f05763b06d1c27c9d3 to your computer and use it in GitHub Desktop.
Bash script to produce two different latex tables from the same cvs (using awk).
key01 name01 description01
key02 name02 description02
key03 name03 description03
#!/bin/bash
input_file='campos.csv'
tablename_prefix='tabela-'
summary_table_filename='tabela-tudo.tex'
# Gera arquivo ${tablename_prefix}key.tex para cada linha em
# `$input_file` (keys devem ser diferentes).
cat $input_file | while read line
do
# Extrai primeiro campo (key) da linha.
key=`echo $line | awk -F, '{print $1}'`
# Reinicia o arquivo `${tablename_prefix}${key}.tex` com o cabeçalho da tabela
echo '\begin{tabular}{cl}' > ${tablename_prefix}${key}.tex
echo '\textbf{key} & \textbf{description}\\\hline' >> ${tablename_prefix}${key}.tex
# Extrai os campos 1 e 3 (key e description) da linha
# acrescenta essas informações no fim de `${tablename_prefix}${key}.tex`.
echo $line | awk -F, '{print $1" & "$3"\\\\"}' >> ${tablename_prefix}${key}.tex
# Conclui a construção da tabela
echo '\end{tabular}' >> ${tablename_prefix}${key}.tex
done
# Gera tabela com todas as linhas
echo '\begin{tabular}{cl}' > ${summary_table_filename}
echo '\textbf{key} & \textbf{name}\\\hline' >> ${summary_table_filename}
awk -F, '{print $1" & "$2"\\\\"}' $input_file >> ${summary_table_filename}
echo '\hline\end{tabular}' >> ${summary_table_filename}
texto.pdf: texto.tex tabela-tudo.tex
pdflatex texto.tex
pdflatex texto.tex
tabela-tudo.tex: campos.csv
./gera-tabelas.sh
\documentclass{article}
% Input and output encoding
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Language: Portuguese (Brazil)
\usepackage[brazil]{babel}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Command to isert a table of a single item,
% referred to by its key.
% Its parameters are
% - the key,
% - the caption,
% - and the label.
\newcommand{\insertItemTable}[3]{%
\begin{table}[h!]
\caption{#2}\label{#3}
\input{tabela-#1}%
\end{table}
}%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Command to insert the summmary table.
\newcommand{\insertSummaryTable}{%
\begin{table}[h!]
\caption{Tabela com todas as informações.}\label{tab:resumo}
\input{tabela-tudo}%
\end{table}
}%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
Usando os labels, podemos referenciar as tabelas: \ref{tab:item-key02}
(mas é preciso processar o texto duas vezes pelo \LaTeX).
\insertItemTable{key01}{Tabela do item `key01'}{tab:item-key01}
\insertItemTable{key02}{Tabela do item `key02'}{tab:item-key02}
\insertItemTable{key03}{Tabela do item `key03'}{tab:item-key03}
\bigskip
E a tabela completa.
\insertSummaryTable
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment