LyXTwo tables on the same line with xtable/knitr

Information and discussion about LyX, a WYSIWYM editor, available for Linux, Windows and Mac OS X systems.
Post Reply
Hive
Posts: 11
Joined: Mon Feb 08, 2016 2:55 pm

Two tables on the same line with xtable/knitr

Post by Hive »

Hello everyone.

I would like to get two tables on the same line due to their small size. I generate them with xtable via knitr chunks and they are placed one above the other by default. I tried to use multicol but the tables simply disappear. I also tried to make a simple table with one line and two columns and put the knitr chunks inside but this send me an error.

Does anyone know a way to do this in Lyx ?

Sincerely
Hive

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

scottkosty
Site Moderator
Posts: 542
Joined: Sat Sep 01, 2012 6:38 am

Re: Two tables on the same line with xtable/knitr

Post by scottkosty »

First create a table float: insert > float > table. Then do the same thing. Now you have a subfloat. Create another subfloat (insert a table float inside the parent float) and you have two subfloats. Put your knitr chunks in there.
Hive
Posts: 11
Joined: Mon Feb 08, 2016 2:55 pm

Two tables on the same line with xtable/knitr

Post by Hive »

Hi and thanks for the answer.

I tried to do what you said but it still doesn't work. In attach, a mwe of what I did. Maybe I missed something ?

I looked at knitr chunk options but didn't find something usefull to set.
Attachments
mwe_tables.tex
(3.81 KiB) Downloaded 366 times
mwe_tables.lyx
(4.53 KiB) Downloaded 317 times
scottkosty
Site Moderator
Posts: 542
Joined: Sat Sep 01, 2012 6:38 am

Re: Two tables on the same line with xtable/knitr

Post by scottkosty »

I'm happy to take a look but please make a *minimal* example. Read here for more information:

http://wiki.lyx.org/FAQ/MinimalExample

The example that you posted has a lot of preamble code. There are also other document settings that are non-standard. Can you create a new document and try to only modify the document settings that are necessary to reproduce the problem?
Hive
Posts: 11
Joined: Mon Feb 08, 2016 2:55 pm

Re: Two tables on the same line with xtable/knitr

Post by Hive »

Sorry. Here another MWE.
Attachments
mwe_tables.lyx
(3.11 KiB) Downloaded 347 times
scottkosty
Site Moderator
Posts: 542
Joined: Sat Sep 01, 2012 6:38 am

Two tables on the same line with xtable/knitr

Post by scottkosty »

Thanks for taking the time to make a clean MWE!

First, if you look at the source code (View > Source), you will see

Code: Select all

@}
knitr needs to have it's beginning delimiter and end delimiter on new lines. LyX now actually knows that knitr needs to be on its own line if you use the Insert > Custom Insets > Chunk because LyX knows that inside that special inset is knitr code. It does not know whether what is inside ERT is LaTeX or knitr code so it does not ensure that it is on its own line. However, you can work around this by just adding a new line at the end (but inside) your ERTs.

Second, xtable produces a floating environment by default. Since you are having LyX take care of the floats in this case, we need to tell xtable to not use a float (and just produce a fixed table). We do this with:

Code: Select all

print.xtable(m_table, floating = FALSE)
If those changes don't fix the problem for you, make the changes and post a new MWE with the changes I suggest.
Hive
Posts: 11
Joined: Mon Feb 08, 2016 2:55 pm

Re: Two tables on the same line with xtable/knitr

Post by Hive »

Thanks a lot, it works by combining custom insert and "floating=FALSE" xtable option.

I also find another solution with two minipages side by side : insert -> box -> without border (maybe not the exact names, I work on french lyx version). Put your knitr chunk inside and then set the width of your minipages less than 50 in order to let them share the same line of the page.

In attach an example of each cases.

Thank you for our help ;)
Attachments
mwe_tables.lyx
(4.6 KiB) Downloaded 375 times
Post Reply