6

I'd like to use an external .txt file to provide a table in my LaTeX document with data. To achieve this I've taken into consideration the pgfplotstable package and the filecontents package. Both of them enabled me to create a complete table out of the data (still, only after manually removing the [] brackets from my .txt file). But I would like to take only certain data and insert them into an existing table of my document.

The .txt file has [] brackets in it, that could be used as markers for a new entry line?

Example:

\documentclass{scrartcl}
\usepackage{tabularx}
\usepackage[table]{xcolor} 
\usepackage{pgfplotstable, filecontents}

\begin{filecontents*}{data.txt}
Title Data
[Name] Clarissa
[Family Name] Fray
[Age] 16
[Country] USA
[Gender] Female
[Haircolor] Red
\end{filecontents*}

\begin{document} 

%% --------------------- What it should look like (but getting data from file)
\begin{figure}
    \rowcolors{1}{gray!25}{white}
        \begin{tabularx}{\textwidth}{|>{\bfseries}l|X|} \hline
        Name & Clarissa \\ \hline
        Family Name & Fray \\ \hline
        Gender & Female \\ \hline
        Age & 16 \\ \hline
    \end{tabularx}
\end{figure}

%%% --------------------- The best I can get (doesn't compile, because of
%%% the brackets in the .txt file and the space in [Family Name])
%\pgfplotstabletypeset[
%every head row/.style = {
%   before row = {
%       \hline
%       }
%   }, 
%after row = \hline,
%every even row/.style={
%   before row = {
%       \rowcolor[gray]{0.9}
%       }
%},
%columns/Title/.style = {column type=|l, column name=First},
%columns/Data/.style = {column type=|l|, column name=Second},
%col sep = space,
%string type,
%]{data.txt}

\end{document}

Any help is really appreciated, thank you!

  • You could preprocess the file with a tool like sed: sed -r 's/\[//g;s/\]/ \&/g;s/(.*)/\1 \\\\/g' data.txt strips the brackets, inserts an & between the would-be columns` and finishes with each line with a \\ . Each of these steps is demarcated by a ; (e.g., s/(.*)/\1 \\\\/g adds the \\ ), so you can not do one of these things by cutting out that part of the command. – jon Mar 16 '16 at 13:48
  • I think @jon is right, using regular expressions to conform your text file to what pgfplots expects is easier than the other way around. – Bibi Mar 24 '16 at 8:48
  • Thanks for your answers! As I have not used this before, do you know if it is a solution for Windows users as well? – kanra Mar 24 '16 at 8:58
  • I have't used a Windows computer (where I had the permission to install anything) in many years, but it seems like there are many options available: stackoverflow.com/questions/127318/… – jon Mar 24 '16 at 15:30
5

On a normal Windows 10 PC, the following should work from a PowerShell prompt. It may also work on Windows 7 or 8, but I can't test that easily right now:

cat .\data.txt | select -skip 1 | % { $_ -replace "\[", "" } | % { $_ -replace "\]", "," } | Out-File -encoding ascii data2.txt

Annotated version:

  • cat .\data.txt gets all the content of data.txt and sends it to the next command in the pipeline
  • | select -skip 1 skips the first line (your headers) from the piped input
  • | % { $_ -replace "[", "" } replaces open bracket characters with empty strings
  • % { $_ -replace "]", "," } replaces close bracket characters with commas
  • | Out-File -encoding ascii data2.txt writes the pipeline data into a file data2.txt in plan ASCII format

This yields a file data2.txt containing:

Name, Clarissa
Family Name, Fray
Age, 16
Country, USA
Gender, Female
Haircolor, Red

and with a document of:

\documentclass{scrartcl}
\usepackage[table]{xcolor} 
\usepackage{pgfplotstable}

\begin{document} 

\pgfplotstabletypeset[
every head row/.style = {
   before row = {
       \hline
       }
   }, 
after row = \hline,
every even row/.style={
   before row = {
       \rowcolor[gray]{0.9}
       },
},
columns/Title/.style = {column type=|l, column name=First},
columns/Data/.style = {column type=|l|, column name=Second},
col sep = comma,
string type,
]{data2.txt}

\end{document}

you get output of:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.