2

I would like to right align text in columns in Sublime Text 3 (on Win 8.1, FWIW).

That is, I want to convert this

Time    Cash Flow
0   -500
1   100
2   200
3   300

into this

 Time   Cash Flow
    0        -500
    1         100
    2         200
    3         300

with the ultimate goal of this

 Time   Cash Flow
-----  ----------
    0        -500
    1         100
    2         200
    3         300

This a simple table in pandoc. I tried the alignment and AlignTab packages, but these seem to specialize in aligning some delimiters rather than aligning columns like I need.

Is there some trick to do this? I am also open to a solution that requires vi or Excel.

1
  • 1
    Did you see my answer? It should be very easy to adapt it so that the specific use-case you introduced in your question is solved. My example expanded your use case quite a bit, in order to demo the full powers of this approached. You may easily "loose the sight on the whole wood because of all the many trees" therefore... May 27, 2015 at 10:30

2 Answers 2

1

Ok, here is a solution which involves Excel or any other spreadsheet program that is able to export CVS.


You have to install a newly released third-party filter named "csv2table" (<- GitHub repository) for Pandoc.

It is also available as a standalone filter.

  1. Install the standalone filter like this:

    mkdir ${HOME}/bin
    
    wget https://gist.githubusercontent.com/baig/b69e3146251bd90d12e7/raw/d8c8171fbe0ad5543f8aaf0a134250b29c689e57/csv2table.hs \
      -O ${HOME}/bin/pandoc-csv2table.hs
    
    chmod a+x ${HOME}/bin/pandoc-csv2table.hs
    
    export PATH=${HOME}/bin:${PATH}
    
  2. My preferred installation method however is via cabal:

    cabal update
    export PATH=${HOME}/.cabal/bin:${PATH}
    cabal install cabal-install
    cabal install pandoc-csv2table
    

Read the csv2table documentation.

What can you do with this filter?

  1. You can include an external CSV file into your Markdown by...

    • ...either abusing the image including syntax like this:

      ![](/path/to/file.csv)

    • ...or by abusing the fenced code block syntax like this:

      ```` {.table source="/path/to/file.csv" <list-of-attributes>}
      ````
      
    • ...or by directly inserting your CSV lines into the "fenced table section" like this:

      ```` {.table <list-of-attributes>}
      one,two,three
      1,2,3
      100,2000,55555
      ````
      
  2. By invoking the filter within the Pandoc command line as
    --filter=pandoc-csv2table
    the CSV will be converted to a table in all your output formats.

  3. Plus, you can apply nearly all the formatting to the table(s) that is supported by Pandoc:

    • Use the attribute type="multiline" to create a multiline_table.
    • Use type="pipe" to create a pipe_table.
    • Use type="simple" to create a simple_table.
    • Use caption="My ***important*** caption for this table" to add a table caption. (This can include markdown formatting.)
    • Use header="no" if you want a table without column headers (default value used, if not given, is header="yes").
    • Now the feature you want: Use aligns=LRCDR to order the alignment of columns (left to right) as Left, Right, Center, Default and Right again.

Example

  1. Consider this my.csv file:

    Time,"*Cash* Flow","Third column" 0,-500,loooooooooooong data field with many words 1,*100*, ***2***,`2000`,shorte **3**,30000,(second line is empty)

    (As you can see, I even use Markdown markup for some of the data fields of the CSV lines!)

  2. Consider this Markdown, my.md:

     # Headline
    
     ```` {.table source="./my.csv" aligns="LRCR" type="multiline" caption="Some ***caption***"}
     ````
    
  3. Run this command to generate Markdown multiline table output from your CSV input:

    pandoc my.md --filter=pandoc-csv2table -t markdown
    

    See this output:

    Screenshot of Markdown output

  4. Run this command to generate PDF output with a multiline table from your CSV input:

    pandoc my.md --filter=pandoc-csv2table -o my.pdf
    

    See this output (screenshot):

    Screenshot of PDF output

  5. If you want to generate "pipe tables" with the alignment-indicating colons syntax, you have to modify the Markdown a bit, as well as the command line:

    • Modify Markdown to include type="pipe" in the table formatting options:

       # Headline
      
       ```` {.table source="./my.csv" aligns="LRCR" type="pipe" caption="Some ***caption***"}
       ````
      
    • Modify command line to ask for markdown_phpextra+table_captions output:

      pandoc my.md --filter=pandoc-csv2table -t markdown_phpextra+table_captions

      See the output now:

      Screenshot with <code>pipe_tables</code> output including alignment-"colons"

  6. Of course, this works with all other output formats too!

1

I have another solution. Pandoc pipe_tables require the following format and can handle left, center, and right alignments.

| Right | Left | Default | Center |
|------:|:-----|---------|:------:|
|   12  |  12  |    12   |    12  |
|  123  |  123 |   123   |   123  |
|    1  |    1 |     1   |     1  |

  : Demonstration of pipe table syntax.

If I separate the columns with pipes |, then when I'm done I can

  • first align the pipes with the alignment package in Sublime Text 3
  • then manually add back the colons : to get the desired alignment.

This isn't quite as easy as @Kurt's filter (and not anywhere near as robust :) ), but it is fairly straight forward.

5
  • Oh, I mis-understood your question then. I thought you'd wanted an easy way to edit your text columns in a text editor so they are aligned. I thought you already know about the different Pandoc/Markdown table formats... My answer showed that you do not need to manually format in an editor, and that you could tell Pandoc to generate Markdown tables from CSV. -- Anyway, there are more tricks to learn if you read man pandoc_markdown. May 27, 2015 at 12:22
  • See also my updated answer. I included an example about how to generate a pipe_table with alignment colons. May 27, 2015 at 12:49
  • 1
    BTW, your "@Kurt's filter" is an honorable mention, but not factually correct :) -- I'm not the author of that filter, Wasif Hasan Baig is. May 27, 2015 at 12:54
  • @KurtPfeifle your answer is perfect! It is actually the answer to the question I should have asked! I don't care what it looks like in markdown only what it looks like in pdf. :) And your answer gets me that anyways with -o markdown. :) Thanks for the lesson! May 27, 2015 at 13:08
  • BTW, I didn't recommend -o markdown, but -o somename.pdf :) May 27, 2015 at 14:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .