LyXTable Challenge

Information and discussion about LyX, a WYSIWYM editor, available for Linux, Windows and Mac OS X systems.
stevesb
Posts: 9
Joined: Tue Jan 19, 2010 11:26 pm

Table Challenge

Post by stevesb »

Dear list

As a newbie with Lyx/Latex I would like to know if there is any way that the following can be accomplished.

I currently have some R code which produces amongst other things a table with about 80 rows or more in 5 col.

Since this does not fit on an A4 page, I split the table in Excel and then copy / paste into Word.

I have used Lyx to do some work and have managed to get Sweave working with Lyx so would like to now try and automate the whole process.

My question is can Lyx\Latex layout the table in the same way as I currently do by hand withou any manual intervention.

Please see the attachment for a sample of the before and after of what I do.

Any assistance in this regard wouold be welcomed.

Regards
Steve
Attachments
TableChallenge.pdf
(25.9 KiB) Downloaded 337 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

Table Challenge

Post by josephwright »

The table looks pretty easy. Getting the data into LaTeX would probably be best using datatool or pgfplotstable, with the raw data exported by Excel as a CSV or tab-separated file. Options for the table itself are pgfplotstable (again) or siunitx.

A small example version (in LaTeX) would be:

Code: Select all

\documentclass{article}
\usepackage{booktabs,siunitx}
\begin{document}
\sisetup{tabformat=-1.2}
\begin{tabular}{lSSSS}
  \toprule
  ID    & {{A}} & {{B}} & {ZBW} & {ZWI} \\ 
  \midrule
  4er66 & 2.30   & 2.30   & 2.44    & 0.00    \\
  4gcyi & 1.82   & 1.88   & -0.07   & -1.24   \\
  5ycya & {{\#}} & {{\#}} & {N/A}   & {N/A}   \\
  \bottomrule
\end{tabular}
\end{document}
As I say, you can include the data automatically using for example datatool. If you could post an example CSV I can probably do a more complete example.
Last edited by josephwright on Wed Jan 20, 2010 11:13 pm, edited 1 time in total.
Joseph Wright
stevesb
Posts: 9
Joined: Tue Jan 19, 2010 11:26 pm

Re: Table Challenge

Post by stevesb »

Hi Joseph

Thanks for the reply and the interest.

I have attached a sample data file in single five col format.

If you have the time to try and get it into the format I need that would be great.

Regards
Steve
Attachments
SampleDataSingleCol.csv
(1.86 KiB) Downloaded 308 times
josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

Table Challenge

Post by josephwright »

Perhaps

Code: Select all

\documentclass{article}
\usepackage{booktabs,colortbl,datatool,siunitx,xtab}
\DTLloadrawdb{data}{SampleDataSingleCol.csv}
\begin{document}
\begin{center}
  \sisetup{dp=2,numdiv=,tabformat=-1.2}
  \begin{xtabular}{lSSSS@{}l}
    \toprule
      ID & A & B & ZBW & ZWI &
    \DTLforeach*{data}{%
      \DTLlabid=labid,%
      \DTLA=A,%
      \DTLB=B,%
      \DTLZBW=ZBW,%
      \DTLZWI=ZWI%
    }
    {%
      \DTLiffirstrow {\\ \midrule}
      {\\ \DTLifoddrow{}{\rowcolor[gray]{.9}}}%
      \DTLlabid & \DTLA & \DTLB & \DTLZBW & \DTLZWI &
    } \\
    \bottomrule
  \end{xtabular}
\end{center}
\end{document}
You'll need to make a very slight edit to your raw data: the first column needs to be called "labid" here, with no underscore.
Joseph Wright
stevesb
Posts: 9
Joined: Tue Jan 19, 2010 11:26 pm

Re: Table Challenge

Post by stevesb »

Hi Joseph

Thanks fot the reply

Looking at the datatool manual, which is very comprehensive, I found the example with the split table. Unfortunatley it's very difficult with Latex to really track what all the functions do, so many thanks for spotting my request and putting two and two together.

I need to update MikTex/Lyx so that I can use datatool and the other packages referred to in you example, but once I have done so I will try your suggested code and see how it goes.

Once again thanks for your interest

Regards
Steve
josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

Re: Table Challenge

Post by josephwright »

That's what things like The LaTeX Companion are for, but datatool, siunitx and pgfplotstable are all too new to appear there. So there is a place for asking about!
Joseph Wright
stevesb
Posts: 9
Joined: Tue Jan 19, 2010 11:26 pm

Re: Table Challenge

Post by stevesb »

Hi Joseph

Sorry but I am back again.

The attached file is the output that I get after running your code. I made the change to lab_id to labid as per your last post

No error messages but it seems clear to me that the \DTLloadrawdb{data} does not work as intended. I have the csv file in the same directory as the Lyx file.

Should it be in another folder ?

I am using Lyx 1.6.2 and MikTex 2.7

Any ideas

Regards
Steve
Attachments
TableChallenge1.pdf
(3.78 KiB) Downloaded 329 times
josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

Re: Table Challenge

Post by josephwright »

Not being a Lyx user, I'm a bit limited on what I can suggest. I used exactly the LaTeX code I posted here, with the CSV file edited to use "labid" with no underscore. The attached PDF is the result using an up to date TeX Live 2009 system. Do you get any errors in the log?
Attachments
untitled.pdf
Typeset table
(38.17 KiB) Downloaded 253 times
Joseph Wright
stevesb
Posts: 9
Joined: Tue Jan 19, 2010 11:26 pm

Re: Table Challenge

Post by stevesb »

Interesting!!!

I have taken a look at the log and from what I can see the following is the problem, as I suspected, but unfortunately I am not sure why.

"Package datatool Warning: File `SampleDataSingleCol.csv' has no data on input l
ine 21."

I have also attached the log, Maybe you can see something else.

Also I notice that there is only single set of col's in your pdf, I would have thought that there was going to be two as per the datatool manual pg47.

Thanks once again for your interest.

Regards
Steve
Attachments
lyx-latex log ver1.txt
(13.91 KiB) Downloaded 280 times
josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

Table Challenge

Post by josephwright »

I suspect that you've got the CSV file open in something like Excel, so it can't be read by TeX. I've attached the CSV file I used (including the edit): try that one.

On the two-column question, it's a bit challenging but can be done.

Code: Select all

\documentclass{article}
\usepackage{booktabs,colortbl,datatool,siunitx}
\DTLloadrawdb{data}{SampleDataSingleCol.csv}
\begin{document}
\begin{center}
  \sisetup{dp=2,numdiv=,tabformat=-1.2}
  \begin{tabular}{lSSSSlSSSS@{}l}
    \toprule
      ID & A & B & ZBW & ZWI & ID & A & B & ZBW & ZWI &
    \DTLforeach*{data}{%
      \DTLlabid=labid,%
      \DTLA=A,%
      \DTLB=B,%
      \DTLZBW=ZBW,%
      \DTLZWI=ZWI%
    }
    {%
      \DTLifoddrow
        {%
          \DTLiffirstrow{\\\midrule}{\\}%
           \ifodd \numexpr (\value{DTLrowi} - 1)/2 \relax 
             \rowcolor[gray]{.9} 
           \fi
        }
        {}%
      \DTLlabid & \DTLA & \DTLB & \DTLZBW & \DTLZWI &
    } \\
    \bottomrule
  \end{tabular}
\end{center}
\end{document}
The most awkward part is the row number: you can't just use \DTLifoddrow, so instead there has to be a calculation which I've based on the low-level counter datatool uses for rows. The attached PDF is the result: I hope it suits.
Attachments
temp.pdf
Two columns and coloured
(37.48 KiB) Downloaded 258 times
SampleDataSingleCol.csv
The raw data, slightly edited
(1.65 KiB) Downloaded 307 times
Joseph Wright
Post Reply