Re: [yWriter] LaTeX output (New beta uploaded)

155 views
Skip to first unread message

Simon H

unread,
Jun 22, 2011, 11:08:48 AM6/22/11
to ywr...@googlegroups.com

It's 11pm here so probably not the best time to upload a new beta, but there you go.

You're welcome to try the new beta out overnight but I won't be here to help.

http://www.spacejock.com/yWriter5_Beta.html

Project - Export - Export to LaTeX is the new setting. There's also a LaTeX setting form where you can rewrite the LaTeX header.

Currently I'm taking the raw RTF code and stripping out the formatting. It's very likely there are many files where this won't work 100%

Cheers
Simon
--
Software designer & programmer
+ author of the Hal Spacejock series
+ Signed copies and ebooks: http://www.spacejock.com.au/BuyHal.html

Adam

unread,
Jun 22, 2011, 11:22:13 PM6/22/11
to yWriter
Might I suggest:
\documentclass[9pt,mlargecrownvopaper,english,onecolumn,twoside,titlepage,extrafontsizes]
{memoir}

English can of course be replaced. The point is, I found 9 point with
mlargecrownvopaper paper size to very closely mimic one of the
standard publishing sizes and words per page.

Now, here comes a giant paste of a perl script which I worked on with
another individual (sorry, don't have the name on record to give
credit). There are more good LaTeX settings and techniques
interspersed throughout.



#!perl -w

use feature "switch";

use XML::Simple;
use XML::DOM;
use RTF::HTMLConverter;
use RTF::Lexer;
use HTML::Parser;
use Data::Dumper;
use File::Basename;
use File::Spec;
use Getopt::Long;

my $file;
my $latex = "C:/Program Files (x86)/MiKTeX 2.8/miktex/bin/
pdflatex.exe";
my $lopts = "-interaction=nonstopmode"; # -interaction=nonstopmode
my $viewer = "C:/Program Files (x86)/Adobe/Reader 9.0/Reader/
AcroRd32.exe";
my $printed;
my $outfile;
my $title;
my $author;
my $language = "english";
#so call with: perl ywriter2pdf.pl --input="C:\Users\Adam\...
\whatever.yw5" --title="Whatever"
#outfile is the tex file, printed is the pdf
GetOptions(
'input=s' => \$file,
'latex=s' => \$latex,
'viewer=s' => \$viewer,
'lopts=s' => \$lopts,
'printed=s' => \$printed,
'outfile=s' => \$outfile,
'title=s' => \$title,
'language=s' => \$language,
);

if ( !defined $file ) {
die if ( $#ARGV < 0 );
$file = $ARGV[0];
}

my $path = dirname($file);
my $base = basename($file);
$base =~ s/\....$//;

if ( !defined $printed ) {
$printed = File::Spec->catfile( ($path), $base . '.pdf' );
}

if ( !defined $title ) {
$title = $base;

#$title =~ s/[\.-_\$]/ /g;
$title =~ s/\./ /g;
$title =~ s/-/ /g;
$title =~ s/_/ /g;
$title =~ s/\$/ /g;
}

if ( !defined $outfile ) {
$outfile = File::Spec->catfile( ($path), $base . '.tex' );
}

my $yW = XMLin($file);
my $chapters = $yW->{'CHAPTERS'}->{'CHAPTER'};
my $scenes = $yW->{'SCENES'}->{'SCENE'};

if ( !defined $author ) {
$author = $yW->{PROJECT}->{AuthorName};
}

my %scene = ();
foreach (@$scenes) {
$scene{ $_->{ID} } = $_;
}

sub preamble {
open( OUT, '>:encoding(utf-8)', $outfile );

#use either ***ebook (6inx9in) or ***mlargecrownvopaper (5.12x7.8in)
[my favorite, then save pdf to 5x8 page). Also pottvopaper
(6.25x4in), mcrownvopaper (4.84x7.32in), a6paper (4.13x5.83)
my $pre = '
\documentclass[9pt,mlargecrownvopaper,' . $language .
',onecolumn,twoside,titlepage,extrafontsizes]{memoir}

\usepackage[' . $language . ']{babel}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{ucs}
\usepackage{makeidx}
\usepackage{quotmark}
\newcommand{\gqt}[1]{\tqt{\scshape #1}}
\newcommand{\stars}{\begin{center} * * *\\\\ \end{center}}

\author{' . $author . '}
\title{' . $title . '}
\begin{document}

\frontmatter
\maketitle

\mainmatter
\chapterstyle{companion}
\book{' . $title . '}

';
print OUT $pre;
}

sub postamble {
my $post = '
\backmatter
\appendix


\end{document}
';
print OUT $post;
close OUT;
}

my $sc_text;
my $para;

sub prefile {

}

sub filter {
my $t = shift;
$t =~ s/<!--.+?-->//gs;
while ( $t =~ s/«\s*([^»]+?)\s*»/\\tqt{$1}/gs ) { }
$t =~ s/&nbsp;//gs;
$t =~ s/"([^"]+)"/\\tqt{'$1'}/gs;

$t =~ s/&ldquo;\s*([^&]+?)\s*&rdquo;/\\tqt{$1}/gs;
$t =~ s/&rdquo;\s*([^&]+?)\s*&rdquo;/\\tqt{$1}/gs;
$t =~ s/&quot;\s*([^&]+?)\s*&quot;/\\tqt{$1}/gs;
$t =~ s/&lt;&lt;&lt;/\\gqt{/g;
$t =~ s/&gt;&gt;&gt;/}/g;
$t =~ s/&lt;&lt;/\\tqt{/g;
$t =~ s/&gt;&gt;/}/g;
$t =~ s/\s?\.\s?\.\s?\.\s?/\\ldots\\ /gs;

$t =~ s/\x92/'/gs;
$t =~ s/\x93/``/gs;
$t =~ s/\x94/''/gs;
$t =~ s/\s-\s/ --- /gs;

$t =~ s/&rquot;/'/gs;
$t =~ s/&rsquo;/'/gs;
$t =~ s/&lquot;/`/gs;
$t =~ s/&lsquo;/`/gs;

#unmatched quotes
$t =~ s/&quot;/``/gs;

if ( !( $t =~ /^\s*$/s ) ) {
$sc_text .= $t . "\n\n";
}
}

sub postfile {

}

sub start {
my ( $n, @a ) = shift;
given ($n) {
when ('html') {

}
when ('head') {

}
when ('style') {

}
when ('body') {

}
when ('p') {
$para = "";
}
when ('i') {
$para .= "\\emph{";
}
when ('font') {

}
default {
$sc_text .= "\n\% unhandled start tag '$n'\n";
}
}
}

sub comment {
my $t = shift;
$t =~ s/\n/ /gs;
$sc_text .= "\%\% $t\n";
}

sub text {
my $t = shift;
$para .= $t;
}

sub end {
my $n = shift;
given ($n) {
when ('html') {

}
when ('head') {

}
when ('style') {

}
when ('body') {

}
when ('p') {
filter($para);
}
when ('i') {
$para .= "}";
}
when ('font') {

}
default {
$sc_text .= "\n\% unhandled end tag '$n'\n";
}
}
}

sub html2latex {
my $html = shift;
my @t = split /\n/, $html;
foreach (@t) {
chomp;
print OUT "\%\%\% $_\n";
}
my $p = new HTML::Parser(
api_version => 3,
comment_h => [ \&comment, "text" ],
start_h => [ \&start, "tagname, attr" ],
text_h => [ \&text, "text" ],
end_h => [ \&end, "tagname" ],
marked_sections => 1
);
$p->empty_element_tags(1);
$p->strict_comment(1);
$p->parse($html);
$p->eof;
}

my $first_in_ch = 0;

sub do_scene {
my $sc = shift;

$sc_text = "";
if ($first_in_ch) {
$first_in_ch = 0;
}
else {
$sc_text .= "\\stars\n\n";
}
$sc_text .= "\%scene: '$sc->{Title}'\n";
my $str = '';
my $rtf = File::Spec->catfile( ( $path, 'RTF5' ), $sc->{RTFFile} );
open my $in, $rtf or die;
my $cvt = RTF::HTMLConverter->new(
in => $in,
out => \$str,
DOMImplementation => 'XML::DOM',
discard_images => 1
);

$cvt->parse();
close $in;
html2latex($str);

$sc_text .= "\n\n";

print OUT $sc_text;
}

sub do_chapter {
my $ch = shift;

print OUT "\\chapter{$ch->{Title}}\n\n";
$first_in_ch = 1;

my $i = 0;
my $nn = $ch->{Scenes}->{ScID};
if ( defined $nn ) {
my @sidss = ( $nn, @$nn );
foreach $sid (@sidss) {
my $s = $scene{$sid};
if ( defined $s ) {
$i++;
print " $i) '$s->{Title}'\n";
print " $s->{RTFFile}\n";
do_scene($s);
}
}
}
}

preamble();

foreach $chapter (@$chapters) {
print "==> '$chapter->{Title}'\n";
do_chapter($chapter);
print "\n";
}

postamble();

system "\"$latex\" $lopts -output-directory=\"$path\" \"$outfile\" ";

#system "\"$latex\" $lopts -output-directory=\"$path\" \"$outfile\" ";
#system "\"$latex\" $lopts -output-directory=\"$path\" \"$outfile\" ";
#print "About to execute: [\"$viewer\" \"$printed\"] ";
system "\"$viewer\" \"$printed\" ";

#latex -interaction=nonstopmode %.tex|bibtex %.aux|latex -
interaction=nonstopmode %.tex|latex -interaction=nonstopmode %.tex|
xdvi %.dvi

Adam

unread,
Jun 22, 2011, 11:26:01 PM6/22/11
to yWriter
Note that this also allows for exporting to .pdf, rather than
just .tex.

On Jun 22, 9:22 pm, Adam <adamcol...@gmail.com> wrote:
> Might I suggest:
> \documentclass[9pt,mlargecrownvopaper,english,onecolumn,twoside,titlepage,e xtrafontsizes]

Simon H

unread,
Jun 22, 2011, 11:26:05 PM6/22/11
to ywr...@googlegroups.com

Adam wrote:

> Might I suggest:
> \documentclass[9pt,mlargecrownvopaper,english,onecolumn,twoside,titlepage,extrafontsizes]{memoir}

> English can of course be replaced. The point is, I found 9 point with
> mlargecrownvopaper paper size to very closely mimic one of the
> standard publishing sizes and words per page.

> Now, here comes a giant paste of a perl script which I worked on with
> another individual (sorry, don't have the name on record to give
> credit). There are more good LaTeX settings and techniques
> interspersed throughout.


Thanks for that - I'll dig through the script and see what I can find.

Bear in mind the header and preamble is completely user-configurable and you can paste in whatever you want.

I needed 5x8" pages for CreateSpace (assuming I self-pub this book) so that's why it's using the current paper size.

I've just uploaded another beta which does a better job of export

Adam

unread,
Jun 22, 2011, 11:43:47 PM6/22/11
to yWriter
Credit where credit is due. Said other user was one Mauro C. There
is an old thread on the issue.

On Jun 22, 9:26 pm, "Simon H" <spacej...@gmail.com> wrote:
> Adam wrote:
> > Might I suggest:
> > \documentclass[9pt,mlargecrownvopaper,english,onecolumn,twoside,titlepage,e xtrafontsizes]{memoir}

Adam

unread,
Jun 22, 2011, 11:46:25 PM6/22/11
to yWriter
Sounds good. Good luck with it all.

I know this is something of a bandaid, not intended to be a major
feature ... but perhaps some very simple gui options for choosing
between a few page sizes, fonts, etc. (rather than the manual LaTex
options setting ... or rather, in addition to it). Just a thought.

pdf output form the tex would be *awesome* ... I wouldn't have to run
my perl script anymore ..but the only way I know to do it is with that
MikTex, which is super huge (on the order of 150MB). If you did want
to go there, perhaps just have it only work if that is installed.

On Jun 22, 9:26 pm, "Simon H" <spacej...@gmail.com> wrote:
> Adam wrote:
> > Might I suggest:
> > \documentclass[9pt,mlargecrownvopaper,english,onecolumn,twoside,titlepage,e xtrafontsizes]{memoir}

Simon H

unread,
Jun 23, 2011, 12:09:49 AM6/23/11
to ywr...@googlegroups.com

Adam wrote:

> Sounds good. Good luck with it all.

> I know this is something of a bandaid, not intended to be a major
> feature ... but perhaps some very simple gui options for choosing

> beween a few page sizes, fonts, etc. (rather than the manual LaTex


> options setting ... or rather, in addition to it). Just a thought.

That's the plan, but it's too restrictive for a Beta. I've been playing with LaTeX for over a day now, and I still can't quite get the output I want. (Text block is too small relative to the page size, which gives me 44 char per line at 11pt on a page which is almost A5 size.) I've been messing with the margins but then I get errors compiling the file.

The other problem is that settings depend on packages. E.g. the memoirs package is ideal for novels, but has 1001 settings you can configure.


> pdf output form the tex would be *awesome* ... I wouldn't have to run
> my perl script anymore ..but the only way I know to do it is with that
> MikTex, which is super huge (on the order of 150MB). If you did want
> to go there, perhaps just have it only work if that is installed.


When I downloaded MikTex complete it was over 950mb. However, I can double-click the Tex file, press Ctrl+Shift+F5 and there's the PDF.

Simon H

unread,
Jun 23, 2011, 5:34:26 AM6/23/11
to ywr...@googlegroups.com

http://sites.google.com/site/ywritersj/faq/exporting?pli=1

Added a couple of paragraphs to the yWriter wiki talking about the new export, along with links to proTeXt etc.

These are my current settings (5x8" paper). I still need to start page counting from the first page (not chapter 1), suppress roman page numbers on the front matter, alter the page header so there's a lot less info. After that it's off to work out how to include images.

\documentclass[11pt,openright,twoside,postvopaper,extrafontsizes]{memoir}
\usepackage{palatino}
\addtolength{\uppermargin}{-0.75in}
\addtolength{\textheight}{1.2in}
\marginparwidth=25pt
\foremargin=50pt
\addtolength{\textwidth}{37pt}
\pdfpagewidth=\paperwidth
\pdfpageheight=\paperheight
\setcounter{tocdepth}{0}
\chapterstyle{demo2}
\setlength{\parindent}{0.1in}
\widowpenalty=1500
\clubpenalty=1500
\midsloppy
\sloppybottom
\checkandfixthelayout

Simon H

unread,
Jun 24, 2011, 12:04:14 AM6/24/11
to ywr...@googlegroups.com

I've added a few extra features: you can start any scene with <TEX> and everything in that scene will be exported to the LaTeX file as-is

(There's a similar command called <HTML> which does the same for html export. And if you put @in front of chapter titles these titles won't appear in the export. This allows you to include copyright pages, title pages, etc in your yWriter project for export to ebook.)

The other thing I've just added for the next beta is inline LaTeX code. You can paste stuff between "<TEX " and " /TEX>" tags just like this:


<TEX
\begin{wrapfigure}{l}{1.45in}
\centering
\includegraphics[width=1.4in]{FeetInHatch.png}
\end{wrapfigure}
/TEX>


That's gets around the problem of yWriter not supporting images ... Just save them all to the main project folder, and the LaTeX export/converter will find them.

Chris....@csiro.au

unread,
Jun 24, 2011, 12:22:48 AM6/24/11
to ywr...@googlegroups.com
Hi Simon,

Not being able to speak any computer language beyond really basic HTML - really basic - will this feature always need some level of programming skill, or will it become user friendly? Ie, push the right buttons and out pops the formatted manuscript?

I think it'd make a really great feature, but if I have to insert code I'll never be able to use it.

Cheers
Chris

--
You received this message because you are subscribed to the Google Groups "yWriter" group.
To post to this group, send email to ywr...@googlegroups.com.
To unsubscribe from this group, send email to ywriter+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ywriter?hl=en.

Simon H

unread,
Jun 24, 2011, 1:15:07 AM6/24/11
to ywr...@googlegroups.com

<Chris....@csiro.au> wrote:

> Hi Simon,

Not being able to speak any computer language beyond really basic HTML - really basic - will this feature always need some level of programming skill, or will it become user friendly? Ie, push the right buttons and out pops the formatted manuscript?

I think it'd make a really great feature, but if I have to insert code I'll never be able to use it.

Cheers
Chris

Attached is the sort of thing you can create without changing a single setting. (I've modified the image to get rid of a double chapter heading - just fixing that now in code.)

HalPg1.png

Peter Barns

unread,
Jun 24, 2011, 1:45:33 PM6/24/11
to ywr...@googlegroups.com
Wow hats off to you Simon. You're doing a grand job.

Peter

Peter Barns

unread,
Jun 24, 2011, 1:50:39 PM6/24/11
to ywr...@googlegroups.com
When it's all sorted we'll all have to send a donation in gratitude. How about it guys?

Peter

Chris....@csiro.au

unread,
Jun 27, 2011, 7:44:04 PM6/27/11
to ywr...@googlegroups.com
Wow. Looks great! Thx Simon.

-----Original Message-----
From: ywr...@googlegroups.com [mailto:ywr...@googlegroups.com] On Behalf Of Simon H

<Chris....@csiro.au> wrote:

> Hi Simon,

Cheers
Chris

Reply all
Reply to author
Forward
0 new messages