## TeX/LaTeX

tips and hints on using BrainStorm with other programs

Moderators: david, OG, Galen

david
Staff
Posts: 329
Joined: Sun Oct 23, 2005 7:08 am
Location: London, England
Contact:

### TeX/LaTeX

We're looking for people with BrainStorm and TeX LaTeX experience. As we learn stuff we'll tack it on to this topic.

(See Q&A topic for the start point)
David Tebbutt

afmcc
Registered member
Posts: 2
Joined: Thu Jun 22, 2006 3:40 am
Location: Birmingham, England
Hello David,

I use LaTeX with Brainstorm for the reasons described in the other post on this subject. LaTeX is a sort of markup language, so although the files are plain ASCII, they can be turned into really beautiful documents by running the latex program on them. With that in mind I wrote a small perl script to turn the text output from Brainstorm into a latex document. I can then use Brainstorm to plan my writing and to work out the outline of the argument, export it as text (which may contain LaTeX code such as \emph{an emphasised part of the text}) and run the brn2latex script on that text file. I then work on the LaTeX document using Emacs/AucTeX (a nice combination for editing LaTeX files) but the creative part has all been done in Brainstorm.

In case this is useful for anyone else, I have cut and pasted the perl code below. Just cut between the dashed lines below, save the copied text in a file 'brn2latex.pl', make sure the AUTHOR environment variable is set to your name, and then (assuming you have perl installed) run:

brn2latex brainstorm_exported_file.txt [optional latex document class]

Best wishes,
Tony

------------------ cut below ------------------------
#!/perl/bin/perl
############################################################
# brn2latex
# Convert a BrainStorm .brn document into a LaTeX document.
#
# Note that the environment variable AUTHOR should be
# set to the name of the document author.
############################################################
use strict;
use warnings;

# Require correct usage
if (@ARGV < 2) {
print "Usage: brn2latex brainstormfile.brn [LaTeX class]\n";
print "The LaTeX document class 'article' is used if no class is specified.\n\n";
}

my $brain =$ARGV[0];
my $docclass =$ARGV[1] || "article";

open(BRAIN, "<", $brain) or die "Can't open the BrainStorm text:$!";

# Prepare the LaTeX document
# Use the same name as the BrainStorm file, but with a .tex suffix in
# place of the .txt suffix. Also replace spaces with underscores in
# the filename.
my $latex; ($latex = $brain) =~ s/\.txt$/.tex/;
$latex =~ tr[ ][_]; open(LATEX, ">",$latex) or die "Can't create LaTeX document file: $!"; print LATEX "\\documentclass[11pt,a4paper]{$docclass}\n";

# The title of the brainstorm model is the first line of the text file
my $title = <BRAIN>; chomp$title;
print LATEX '\title{', $title, "}\n"; print LATEX '\author{',$ENV{AUTHOR}, "}\n";
print LATEX '\begin{document}', "\n";
print LATEX '\maketitle', "\n\n" if $docclass eq "article"; # Section headings my %headings; if (($docclass eq "report") || ($docclass eq "book")) { %headings = ( "\t" => '\chapter', "\t\t" => '\section', "\t\t\t" => '\subsection' ); } else { %headings = ( "\t" => '\section', "\t\t" => '\subsection' ); } # Read lines from the BrainStorm text output file # Use the level of indentation to determine the section heading Ignore # the first indent since the entry with no indentation is simply the # title of the BrainStorm model while (<BRAIN>) { if (/^\s+$/s) {
print LATEX $_; next; } # Replace special characters in the document text s/([&])/\\$1/g; # Ampersand
s/%/\\%/g; # Percent
s/"\b//g; # Opening double quote
s/"/''/g; # Closing double quote
s/'\b/`/g; # Opening single quote
s/ - / --- /g; # Dash becomes mdash

# Separate off the leading tabs
/^([\t]+)(.*)$/; my$text = $2; chomp$text;
if (defined $headings{$1}) { # Is this a section heading?
print LATEX "$headings{$1}", "{$2}\n\n"; } else { print LATEX "$text\n\n"; # Output as a paragraph
}
}
close(BRAIN);

print LATEX '\end{document}', "\n";
close(LATEX);

print "LaTeX file \$latex successfully written.\n";

-------------------- cut above ------------------------

david
Staff
Posts: 329
Joined: Sun Oct 23, 2005 7:08 am
Location: London, England
Contact:
That's mighty kind of you. Thank you.

Are we talking about a tabbed outline or single spaced? Or something else? Sorry, I don't read Perl.
David Tebbutt

afmcc
Registered member
Posts: 2
Joined: Thu Jun 22, 2006 3:40 am
Location: Birmingham, England
Yes, I am assuming that each level of the Brainstorm structure is marked by a leading TAB character in the text file export. The perl script uses the number of TAB characters at the start of a line to deduce the logical structure of the LaTeX document. If the user selects the 'report' or 'book' document classes the first three levels of Brainstorm structure translate into 'chapter', 'section' and 'subsection' level headings in the LaTeX output. If the user selects the 'article' document class (or doesn't specify one at all) then the Brainstorm structure translates into 'section' and 'subsection' logical levels. Everything below these levels becomes a normal paragraph in LaTeX. It may be that in the LaTeX tweaking stage, some lists exported from Brainstorm would end up as LaTeX lists (\items in an enumerate or itemize environment), but that would be added by hand after running brn2latex.

Best wishes,
Tony

alx
Insider
Posts: 46
Joined: Sun Oct 23, 2005 9:24 pm
Tony,

Many thanks for your submission to the forum; a new use has just been created for Brainstorm. I only guessed the potential in theory but it's great to know that someone has actually implemented this.

Are there any TeX/LaTeX forums where you have already posted your script or plan to do so? I imagine that TeX/LaTeX conoisseurs would like to know of the ways that Brainstorm can help them be more productive.

I also have the impression that the existing Brainstorm user base has yet to grasp the TeX/LaTeX potential; I wonder whether you could attach a graphic sample of one of your LaTeX scripts originally created in Brainstorm.

TIA
Alexander

liseely
Registered member
Posts: 3
Joined: Sat Nov 02, 2013 12:46 am

### Re: TeX/LaTeX

Welcome to have more fun game waiting for you

### Who is online

Users browsing this forum: No registered users and 1 guest