-1

I have a text file in the format (space delimited, one entry per line):

row1 val1 val2
row2 val1 val2 

I have the header values for the table within an array, say fieldArray.

I need to create a LATEX table similar to this (added a screenshot below because table is not displaying properly):

header 1 header 2 header 3
row1 val1 val2
row2 val1 val2
row3 val1 val2

In the table above, the headers come from the array, and the 'row' & 'val' comes from the lines in the .txt file.

I need to make the table from the .txt and the fieldArray within my shell script and put it into a file called 'temp.tex', then I need to use pdflatex to create a pdf of the table from that .tex file and call it 'table.pdf' (All within a single script).

Though, in the shellscript, the .txt file is not always the same. The .txt changes depending on what the user inputs when they call the script.

Desired table:

Desired_table

Tory
  • 109
  • 11
  • Not sure why my table is displaying like that? it looked fine in the preview? – Tory Feb 15 '22 at 06:59
  • Welcome on StackOverflow. May I suggest that you take a look at the [help center](https://stackoverflow.com/help) and especially at [How do I format my posts using Markdown or HTML?](https://stackoverflow.com/help/formatting)? – Renaud Pacalet Feb 15 '22 at 07:01
  • There is nothing like `header1`, `header2`, `header3`, `row3` in the input you show. Please try to provide consistent inputs and expected outputs. Moreover, StackOverflow is not a free coding service. You are supposed to also show what you tried, explain why it does not work as you would like. It is only then that we may suggest modifications of your code. – Renaud Pacalet Feb 15 '22 at 07:04
  • the headers (first row only) come from the fieldArray, every other row comes from the .txt @RenaudPacalet sorry if that was confusing. So for example, in the "desired table" img, the 'name' 'exam1' 'exam2' 'exam3' come from the fieldArray not the .txt, also i have fixed the table formatting – Tory Feb 15 '22 at 07:11
  • Still, there is nothing like `row3` in your input. And you still don't show the code with which you need help. – Renaud Pacalet Feb 15 '22 at 07:26
  • the .txt can be of variable size, the amount of content in the rows as well as the amount of rows can be different, depending on which text file is chosen. I will try to come up with something but I honestly don't know where to start thats why I didnt provide any code. – Tory Feb 15 '22 at 07:43
  • Does this oneliner helps you? `while read -r line; do echo $line | sed -e 's/ / \& /g' -e 's/$/\\\\ \\hline/'; done < inp.txt;`. Do you undersand what it does? Are you able to run it? – David Lukas Feb 15 '22 at 08:20
  • 1
    I think I have come up with a solution. First copy the contents of the txt file into a new file called temp.tex. Add a line at the top for the headers (from the fieldArray). Then manipulate the conents off the temp.tex with awk in order to get it to be in LATEX table format. Then use pdflatex to create the PDF. Does this sound feasible? Also I will try the oneliner that David has suggested. – Tory Feb 15 '22 at 09:17

1 Answers1

2

Using pgfplotstable:

\documentclass{article}

\usepackage{pgfplotstable}

\begin{document}

\pgfplotstabletypeset[
    columns/0/.style={column name={header 1}},
    columns/1/.style={column name={header 2}},
    columns/2/.style={column name={header 3}},
    header=false,
    string type,
    before row=\hline,
    every last row/.style={after row=\hline},
    column type/.add={|}{},
    every last column/.style={column type/.add={}{|}}
]{test.txt}%


\end{document}

enter image description here


For an arbitrary number of columns, prepend your header to the .txt file, e.g.

{header 1} {header 2} {header 3}
row1 val1 val2
row2 val1 val2 

and let pgfplotstable do the work for you:

\documentclass{article}

\usepackage{pgfplotstable}

\begin{document}

\pgfplotstabletypeset[
    string type,
    before row=\hline,
    every last row/.style={after row=\hline},
    column type/.add={|}{},
    every last column/.style={column type/.add={}{|}}
]{test.txt}%


\end{document}
  • I went ahead and was just formatting my .txt file into LATEX format with sed, do you think that would be feasible? I have already written sed commands to add the "&"s between the row values, the "\\" at end of each line and the "\hline"s on a new line after each row. (I am not sure that I am allowed to use the "\usepackage{pgfplotstable}" because this is a assignment). And also the number of headers is variable. It is supposed to take any space delimited txt file and create a simple table from it. – Tory Feb 15 '22 at 11:07
  • @Tory Feasible? Yes, but unnecessarily complicate. – samcarter_is_at_topanswers.xyz Feb 15 '22 at 11:08
  • @Tory If the headers are not arbitrary, but for example numbered, this can easily be expanded to an unknown number of columns. – samcarter_is_at_topanswers.xyz Feb 15 '22 at 11:09
  • I have an array with the headers in it. They are not numbered because the headers can be any value. – Tory Feb 15 '22 at 11:13
  • yes, but if you have an array, you know how many there are and can iterate over them. – samcarter_is_at_topanswers.xyz Feb 15 '22 at 11:17
  • What would be really, really easy: prepend the header array to your tex file and then pgfplotstable can automatically get the headers from there – samcarter_is_at_topanswers.xyz Feb 15 '22 at 11:18
  • Yea I added the array to the first line of my tex file, that's the first thing I did. Is pgfplotstable something I have to install? Or is it included in LATEX? I am very new to UNIX and have never used LATEX until now, sorry for all my confusion. – Tory Feb 15 '22 at 11:21
  • @Tory pgfplotstable is available in texlive and miktex. If you have a complete installation of e.g. texlive (which I highly recommend) it will already be installed. – samcarter_is_at_topanswers.xyz Feb 15 '22 at 11:27
  • Is there a command I can use to determine if I have pgfplotstable? I am just skeptical because in my assignment specifications it doesnt mention pgfplotstable, all it says is "You should create a table in a file called tmp.tex. You should not include the comments when generating the table. You should then create a PDF file called table.pdf using the latex and dvipdf utilities. After, you should remove the tmp.tex, tmp.dvi, and any other temporary files and exit." – Tory Feb 15 '22 at 11:30
  • And "While you are allowed to invoke other shell scripts that you have written and predefined Unix utilities, you are not allowed to invoke other executables." – Tory Feb 15 '22 at 11:31
  • The requirements of your assignment are your problem, not ours. We are here to build a library of knowledge so that future users with the same problem can benefit from it, not to solve your homework!!!! – samcarter_is_at_topanswers.xyz Feb 15 '22 at 11:33
  • Understandable. I guess I will just stick to the sed route to get my textfile in proper format. I am going to go ahead and accept your answer. Thanks for all of the help. – Tory Feb 15 '22 at 11:42
  • @Tory I have written a bash solution using sed and without using particular LaTeX packages: if the question will get reopened, I will post it. – Eddymage Feb 15 '22 at 12:08
  • @Eddymage ah that is sad, I doubt it will be reopened. – Tory Feb 15 '22 at 13:06