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!
Best Answer
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:
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 formatThis yields a file data2.txt containing:
and with a document of:
you get output of: