HOME
PDF letter size
PDF legal size

Generating direct Latex using multi-line raw string (or HERE DOCUMENT)

Nasser M. Abbasi

June 16, 2016 page compiled on June 16, 2016 at 11:28pm

Contents

1 Ruby
2 Python
3 Perl
4 C++11

These are small examples showing the use of https://en.wikipedia.org/wiki/Here_document which is also called multi-line raw strings or literal string.

This is done in Perl, Ruby, Python and C++11. These raw strings can contain any text in them, including embedded strings and escape characters. In addition, one can use sprintf like operations on them by replacing some tokens that have  %s in them, with variables after building the strings. The main use of this is to be able to generate Latex directly from another language, such as Mathematica or Perl or Python, etc... since there are cases where it is easier to do this than coding the logic direclty in Latex itself. Some usage examples are below

1 Ruby

Now running the above script t.rb gives

Notice that % in the raw string (the percentage sign) had to also have a place holder to avoid conflict with the % used for string replacement. In the Perl example below, this is not needed.

2 Python

Which is very similar to Ruby. Except in Ruby there was no need to add s after the place holder. Also, the % in the raw string had to be special handled the same as with Ruby.

3 Perl

In Perl however, the %, does not need special handling

Notice how the 100% was left as is. No place holder was needed for it in Perl.

4 C++11

and now

>g++ -Wall -std=c++0x ./t1.cpp
>./a.out

However, the C++ solution is not as flexible as the others, since it does not support named place holders which makes it little harder to replace tokens in the raw string if there are many of them.

References

  1. C++ string literals
  2. Ruby accepted answer here
  3. Perl Named.pm