Pages

02 September, 2015

Copy & paste tabular data to tables in LyX

As it can be cumbersome to manually transfer data from an external source to a table in LyX, here is trick to speed up the process.

Let's assume the data is stored comma-separated in a text file with the following content.
1920,1080,48,24,40,45,1920,1080,0%
1920,1080,46,23,41,46,1886,1058,3.8%
1920,1080,45,23,42,46,1890,1058,3.6%
1920,1080,44,22,43,49,1892,1078,1.6%
1920,1080,43,22,44,49,1892,1078,1.6%
1920,1080,42,21,45,51,1890,1071,2.4%
1920,1080,41,21,46,51,1886,1071,2.6%
1920,1080,40,20,48,54,1920,1080,0.0%
Then copy&pasting (CMD+SHIFT+V or CTRL+SHIFT+V) this text into a table in LyX will only fill the first table column. However, if the data were TAB-separated as follows,
1920 1080 48 24 40 45 1920 1080 0%
1920 1080 46 23 41 46 1886 1058 3.8%
1920 1080 45 23 42 46 1890 1058 3.6%
1920 1080 44 22 43 49 1892 1078 1.6%
1920 1080 43 22 44 49 1892 1078 1.6%
1920 1080 42 21 45 51 1890 1071 2.4%
1920 1080 41 21 46 51 1886 1071 2.6%
1920 1080 40 20 48 54 1920 1080 0.0%
then LyX will nicely fill your table. This should also work for data that is copy pasted from spreadsheet applications.

If your spreadsheet does not copy&paste using TAB-separated data in the clipboard, then you can use some Vim magic. Paste the, e.g., space-separated, data into Vim and replace spaces with tabs, i.e., type :%s# #\t#gc (ENTER).

Note: Don't forget to use format-preserving paste (CMD+SHIFT+V or CTRL+SHIFT+V) instead of the normal paste in LyX (CMD+V or CTRL+V)

PS: I know that LyX also offers to import external files, but I often like to have all my text editable inside the document, e.g., to add colors, change font sizes, add footnotes, or use Math enviroments in the data.

No comments: