1

I have been working on this idea for some time and I thought of sharing this information. Suppose you have some raw data as so

0.7483 0.5526 0.2311 0.1460
0.8346 0.5804 0.2164 0.1100
0.8430 0.5676 0.2089 0.0711

And as you may see this is not suitable to be inserted in a latex table. Is there a way to organise this dataset such that it has $ and & sign between them such that I do not have to regularly pass through them.

  • 1
    I think that reducing the scope of the question to sublime is not needed... – Rmano Oct 18 '17 at 8:23
2

No need to add anything. You can use pgfplotstable:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{pgfplotstable}
\usepackage{booktabs}
\begin{document}
\pgfplotstabletypeset[sci zerofill]{
a b c d
0.7483 0.5526 0.2311 0.1460
0.8346 0.5804 0.2164 0.1100
0.8430 0.5676 0.2089 0.0711
}

formatted table

...you have zillions of options, just check the docs. For example, a quick change:

...
\pgfplotstabletypeset[
    every head row/.style={
        before row=\toprule,after row=\midrule},
    every last row/.style={
        after row=\bottomrule},
    fixed, fixed zerofill, precision=4,
]{
...

Gives:

new table

without touching anything else. In general, I prefer to use the numbers as they are generated by the application that generates them, so that if I had to change them, it's just a copy and paste (or, better, an included file with the data, you can do it with pgfplotstable). Keeping content and format separate is the main reason to use LaTeX...

  • Thank you for this but this is in Scientific notations. Sometimes one needs to avoid this see the 7.11.10^-2. – rsc05 Oct 18 '17 at 8:31
  • 1
    @rsc05 you have plenty of options to control how numbers are formatted when using pgtplotstable. – Lyndon White Oct 18 '17 at 8:44
0

I have figured out the answer to this question and thought about sharing it with you. This involves using regular expression

Step 1: Ctr+h, then Alt+R

Step 2: Change the first number in each line (as it has a different formation) as

Find: ^(\d+.?\d+)
Replace: \$$1\$ \&

Then press on Ctrl+Alt+Enter

Step 3: Then I followed the suggestions of this useful link, I did the following step to insert a $ sign on all numbers except those that has already $ sign or those numbers at the very end of the line.

Find: (\$\d+.?\d+\$|\d+.?\d+$)(*SKIP)(*F)|(\d+.?\d+)
Replace: \$$2\$ \&

Then press on Ctrl+Alt+Enter

Step 4: The only thing that is left is to replace all numbers except those that have already a dollar sign as

Find: \$\d+.?\d+\$(*SKIP)(*F)|(\d+.?\d+)
Replace: \$$1\$

Then press on Ctrl+Alt+Enter

Where this step finds all numbers except those that were already wrapped by a $ sign.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.