]> git.lyx.org Git - lyx.git/blob - lib/reLyX/BasicLyX.pm
When in doubt, do as LyX does
[lyx.git] / lib / reLyX / BasicLyX.pm
1 # This file is part of reLyX
2 # Copyright (c) 1998-9 Amir Karger karger@post.harvard.edu
3 # You are free to use and modify this code under the terms of
4 # the GNU General Public Licence version 2 or later.
5
6 package BasicLyX;
7 # This package includes subroutines to translate "clean" LaTeX to LyX.
8 # It translates only "basic" stuff, which means it doesn't do class-specific
9 #     things. (It uses the TeX parser Text::TeX)
10 use strict;
11
12 use RelyxTable; # Handle LaTeX tables
13 use RelyxFigure; # Handle LaTeX figures
14 use Verbatim;   # Copy stuff verbatim
15
16 use vars qw($bibstyle_insert_string $bibstyle_file $Begin_Inset_Include);
17 $bibstyle_insert_string = "%%%%%Insert bibliographystyle file here!";
18 $bibstyle_file = "";
19 $Begin_Inset_Include = "\\begin_inset Include";
20
21 #################### PACKAGE-WIDE VARIABLES ###########################
22 my $debug_on; # is debugging on?
23
24 ######
25 #Next text starts a new paragraph?
26 # $INP = 0 for plain text not starting a new paragraph
27 # $INP = 1 for text starting a new paragraph, so that we write a new
28 #    \layout command and renew any font changes for the new paragraph
29 # Starts as 1 cuz if the first text in the document is plain text
30 #    (not, e.g., in a \title command) it will start a new paragraph
31 my $IsNewParagraph = 1;
32 my $OldINP; #Save $IsNewParagraph during footnotes
33
34 # Some layouts may have no actual text in them before the next layout
35 # (e.g. slides). Pending Layout is set when we read a command that puts us
36 # in a new layout. If we get some regular text to print out, set it to false.
37 # But if we get to another layout command, first print out the command to
38 # start the pending layout.
39 my $PendingLayout = 0;
40
41 # HACK to protect spaces within optional argument to \item
42 my $protect_spaces = 0;
43
44 # $MBD = 1 if we're in a list, but haven't seen an '\item' command
45 #    In that case, we may need to start a nested "Standard" paragraph
46 my $MayBeDeeper = 0;
47 my $OldMBD; #Save $MBD during footnotes -- this should very rarely be necessary!
48
49 # Stack to take care of environments like Enumerate, Quote
50 # We need a separate stack for footnotes, which have separate layouts etc.
51 # Therefore we use a reference to an array, not just an array
52 my @LayoutStack = ("Standard"); #default if everything else pops off
53 my $CurrentLayoutStack = \@LayoutStack;
54
55 # Status of various font commands
56 # Every font characteristic (family, series, etc.) needs a stack, because
57 #    there may be nested font commands, like \textsf{blah \texttt{blah} blah}
58 # CurrentFontStatus usually points to the main %FontStatus hash, but
59 #     when we're in a footnote, it will point to a temporary hash
60 my %FontStatus = (
61     '\emph' => ["default"],
62     '\family' => ["default"],
63     '\series' => ["default"],
64     '\shape' => ["default"],
65     '\bar' => ["default"],
66     '\size' => ["default"],
67     '\noun' => ["default"],
68 );
69 my $CurrentFontStatus = \%FontStatus;
70
71 # Currently aligning paragraphs right, left, or center?
72 my $CurrentAlignment = "";
73 my $OldAlignment; # Save $AS during footnotes
74
75 # Global variables for copying tex stuff
76 my $tex_mode_string; # string we accumulate tex mode stuff in
77 my @tex_mode_tokens; # stack of tokens which required calling copy_latex_known
78
79 # LyX strings to start and end TeX mode
80 my $start_tex_mode = "\n\\latex latex \n";
81 my $end_tex_mode = "\n\\latex default \n";
82
83 # String to write before each item
84 my $item_preface = "";
85
86 #############  INFORMATION ABOUT LATEX AND LYX   #############################
87 # LyX translations of LaTeX font commands
88 my %FontTransTable = (
89    # Font commands
90    '\emph' => "\n\\emph on \n",
91    '\underline' => "\n\\bar under \n",
92    '\underbar' => "\n\\bar under \n", # plain tex equivalent of underline?
93    '\textbf' => "\n\\series bold \n",
94    '\textmd' => "\n\\series medium \n",
95    '\textsf' => "\n\\family sans \n",
96    '\texttt' => "\n\\family typewriter \n",
97    '\textrm' => "\n\\family roman \n",
98    '\textsc' => "\n\\shape smallcaps \n",
99    '\textsl' => "\n\\shape slanted \n",
100    '\textit' => "\n\\shape italic \n",
101    '\textup' => "\n\\shape up \n",
102    '\noun' => "\n\\noun on \n", # LyX abstraction of smallcaps
103
104 # Font size commands
105    '\tiny' => "\n\\size tiny \n",
106    '\scriptsize' => "\n\\size scriptsize \n",
107    '\footnotesize' => "\n\\size footnotesize \n",
108    '\small' => "\n\\size small \n",
109    '\normalsize' => "\n\\size default \n",
110    '\large' => "\n\\size large \n",
111    '\Large' => "\n\\size Large \n",
112    '\LARGE' => "\n\\size LARGE \n",
113    '\huge' => "\n\\size huge \n",
114    '\Huge' => "\n\\size Huge \n",
115    # This doesn't work yet!
116    #'\textnormal' => "\n\\series medium \n\\family roman \n\\shape up \n",
117 );
118
119 # Things LyX implements as "Floats"
120 my %FloatTransTable = (
121    # Footnote, Margin note
122    '\footnote' => "\n\\begin_float footnote \n",
123    '\thanks' => "\n\\begin_float footnote \n", # thanks is same as footnote
124    '\marginpar' => "\n\\begin_float margin \n",
125 );
126 # Environments that LyX implements as "floats"
127 my %FloatEnvTransTable = (
128     "table" => "\n\\begin_float tab \n",
129     "table*" => "\n\\begin_float wide-tab \n",
130     "figure" => "\n\\begin_float fig \n",
131     "figure*" => "\n\\begin_float wide-fig \n",
132 );
133
134 # Simple LaTeX tokens which are turned into small pieces of LyX text
135 my %TextTokenTransTable = (
136     # LaTeX escaped characters
137     '\_' => '_',
138     '\%' => '%',
139     '\$' => '$',
140     '\&' => '&',
141     '\{' => '{',
142     '\}' => '}',
143     '\#' => '#',
144     '\~' => '~',
145     '\^' => '^',
146     # \i and \j are used in accents. LyX doesn't recognize them in plain
147     #    text. Hopefully, this isn't a problem.
148     '\i' => '\i',
149     '\j' => '\j',
150
151     # Misc simple LaTeX tokens
152     '~'      => "\n\\protected_separator \n",
153     '@'      => "@", # TeX.pm considers this a token, but it's not really
154     '\@'     => "\\SpecialChar \\@",
155     '\ldots' => "\n\\SpecialChar \\ldots\{\}\n",
156     '\-'     => "\\SpecialChar \\-\n",
157     '\LaTeX' => "LaTeX",
158     '\LaTeXe' => "LaTeX2e",
159     '\TeX'    => "TeX",
160     '\LyX'    => "LyX",
161     '\lyxarrow' => "\\SpecialChar \\menuseparator\n",
162     '\hfill'  => "\n\\hfill \n",
163     '\noindent'        => "\n\\noindent \n",
164     '\textbackslash'   => "\n\\backslash \n",
165     '\textgreater'     => ">",
166     '\textless'        => "<",
167     '\textbar'         => "|",
168     '\textasciitilde'  => "~",
169 );
170
171 # LyX translations of some plain LaTeX text (TeX parser won't recognize it
172 #     as a Token, so we need to translate the Text::TeX::Text token.)
173 my %TextTransTable = (
174     # Double quotes
175     "``" => "\n\\begin_inset Quotes eld\n\\end_inset \n\n",
176     "''" => "\n\\begin_inset Quotes erd\n\\end_inset \n\n",
177
178     # Tokens that don't start with a backslash, so parser won't recognize them
179     # (LyX doesn't support them, so we just print them in TeX mode)
180     '?`' => "$start_tex_mode?`$end_tex_mode",
181     '!`' => "$start_tex_mode!`$end_tex_mode",
182 );
183
184 # Things that LyX translates as "LatexCommand"s
185 # E.g., \ref{foo} ->'\begin_inset LatexCommand \ref{foo}\n\n\end_inset \n'
186 # (Some take arguments, others don't)
187 my @LatexCommands = map {"\\$_"} qw(ref pageref label cite bibliography
188                                  index printindex tableofcontents
189                                  listofalgorithms listoftables listoffigures);
190 my @IncludeCommands = map {"\\$_"} qw(input include);
191 # Included postscript files
192 # LyX 1.0 can't do \includegraphics*!
193 my @GraphicsCommands = map {"\\$_"} qw(epsf epsffile epsfbox
194                                        psfig epsfig includegraphics);
195
196 # Accents. Most of these take an argument -- the thing to accent
197 # (\l and \L are handled as InsetLatexAccents, so they go here too)
198 my $AccentTokens = "\\\\[`'^#~=.bcdHklLrtuv\"]";
199
200 # Environments which describe justifying (converted to LyX \align commands)
201 #    and the corresponding LyX commands
202 my %AlignEnvironments = (
203     "center" => "\n\\align center \n",
204     "flushright" => "\n\\align right \n",
205     "flushleft" => "\n\\align left \n",
206 );
207
208 # Some environments' begin commands take an extra argument
209 # Print string followed by arg for each item in the list, or ignore the arg ("")
210 my %ExtraArgEnvironments = (
211     "thebibliography" => "",
212     "lyxlist" =>'\labelwidthstring ',
213     "labeling" =>'\labelwidthstring ', # koma script list
214 );
215
216 # Math environments are copied verbatim
217 my $MathEnvironments = "(math|displaymath|xxalignat|(equation|eqnarray|align|alignat|xalignat|multline|gather)(\\*)?)";
218 # ListLayouts may have standard paragraphs nested inside them.
219 my $ListLayouts = "Itemize|Enumerate|Description";
220
221 # passed a string and an array
222 # returns true if the string is an element of the array.
223 sub foundIn {
224     my $name = shift;
225     return grep {$_ eq $name} @_;
226 }
227
228 my @NatbibCommands = map {"\\$_"} qw(citet citealt citep citealp citeauthor);
229
230 # passed a string.
231 # returns true if it is a valid natbib citation
232 sub isNatbibCitation {
233     my $name = shift;
234
235     # These two have a single form
236     return 1 if ($name eq '\citeyear' or $name eq '\citeyearpar');
237
238     # Natbib citations can start with a 'C' or a 'c'
239     $name =~ s/^\\C/\\c/;
240     # The can end with a '*'
241     $name =~ s/\*$//;
242     # Is this doctored string found in the list of valid commands?
243     return foundIn($name, @NatbibCommands);
244     
245 }
246
247 #####################   PARSER INVOCATION   ##################################
248 sub call_parser {
249 # This subroutine calls the TeX parser & translator
250 # Before it does that, it does lots of setup work to get ready for parsing.
251 # Arg0 is the file to read (clean) LaTeX from
252 # Arg1 is the file to write LyX to
253 # Arg2 is the file to read layouts from (e.g., in LYX_DIR/layouts/foo.layout)
254
255     my ($InFileName, $OutFileName) = (shift,shift);
256
257 # Before anything else, set the package-wide variables based on the
258 #    user-given flags
259     # opt_d is set to 1 if '-d' option given, else (probably) undefined
260     $debug_on = (defined($main::opt_d) && $main::opt_d);
261
262     # Hash of tokens passed to the TeX parser
263     # Many values are $Text::TeX::Tokens{'\operatorname'}, which has
264     #    Type=>report_args and count=>1
265     # Note that we don't have to bother putting in tokens which will be simply
266     #    translated (e.g., from %TextTokenTransTable).
267     my %MyTokens = ( 
268         '{' => $Text::TeX::Tokens{'{'},
269         '}' => $Text::TeX::Tokens{'}'},
270         '\begin' => $Text::TeX::Tokens{'\begin'},
271         '\end' => $Text::TeX::Tokens{'\end'},
272
273         # Lots of other commands will be made by ReadCommands:Merge
274         # by reading the LaTeX syntax file
275
276         # Font sizing commands (local)
277         '\tiny' => {Type => 'local'},
278         '\small' => {Type => 'local'},
279         '\scriptsize' => {Type => 'local'},
280         '\footnotesize' => {Type => 'local'},
281         '\small' => {Type => 'local'},
282         '\normalsize' => {Type => 'local'},
283         '\large' => {Type => 'local'},
284         '\Large' => {Type => 'local'},
285         '\LARGE' => {Type => 'local'},
286         '\huge' => {Type => 'local'},
287         '\Huge' => {Type => 'local'},
288
289         # Tokens to ignore (which make a new paragraph)
290         # Just pretend they actually ARE new paragraph markers!
291         '\maketitle' => {'class' => 'Text::TeX::Paragraph'},
292     );
293     
294     # Now add to MyTokens all of the commands that were read from the
295     #    commands file by package ReadCommands
296     &ReadCommands::Merge(\%MyTokens);
297
298 # Here's the actual subroutine. The above is all preparation
299     # Output LyX file
300     my $zzz = $debug_on ? " ($InFileName --> $OutFileName)\n" : "... ";
301     print STDERR "Translating$zzz";
302     open (OUTFILE,">$OutFileName");
303
304     # Open the file to turn into LyX.
305     my $infile = new Text::TeX::OpenFile $InFileName,
306         'defaultact' => \&basic_lyx,
307         'tokens' => \%MyTokens;
308
309     # Process what's left of the file (everything from \begin{document})
310     $infile->process;
311
312     # Last line of the LyX file
313     print OUTFILE "\n\\the_end\n";
314     close OUTFILE;
315     #warn "Done with basic translation\n";
316     return;
317 } # end subroutine call_parser
318
319 # This is used as a toggle so that we know what to do when basic_lyx is
320 # passed a '$' or '$$' token.
321 my $inside_math=0;
322
323 sub starting_math {
324     my $name = shift;
325
326     if ($name eq '\(' || $name eq '\[' ||
327         # These tokens bound both ends of a math environment so we must check
328         # $inside_math to know what action to take.
329         ($name eq '$' || $name eq '$$') && !$inside_math) {
330
331         $inside_math = 1;
332         return 1;
333     }
334
335     # All other tokens
336     return 0;
337 }
338
339 sub ending_math {
340     my $name = shift;
341
342     if ($name eq '\)' || $name eq '\]' ||
343         # These tokens bound both ends of a math environment so we must check
344         # $inside_math to know what action to take.
345         ($name eq '$' || $name eq '$$') && $inside_math) {
346
347         $inside_math = 0;
348         return 1;
349     }
350
351     # All other tokens
352     return 0;
353 }
354
355
356 # Straight translation of LaTeX lengths to LyX ones.
357 my %lengthAsLyXString = ('\textwidth' => 'text%',
358                          '\columnwidth' => 'col%',
359                          '\paperwidth' => 'page%',
360                          '\linewidth' => 'line%',
361                          '\paperheight' => 'pheight%',
362                          '\textheight' => 'theight%');
363
364 sub getAsLyXLength {
365     my $LatexLength = shift;
366
367     my $LyXLength = '';
368     # If $LatexLength is something like '4.5\columnwidth', translate into
369     # LyXese.
370     if ($LatexLength =~ /([0-9]+\.?[0-9]*)\s*(\\[a-z]*)/) {
371         if (defined($lengthAsLyXString{$2})) {
372             $LyXLength = ($1 * 100) . $lengthAsLyXString{$2};
373         }
374     } else {
375         $LyXLength = $LatexLength;
376         # Remove any spaces from '4.5 cm'
377         $LyXLength =~ s/\s*//g;
378     }
379
380     return $LyXLength;
381 }
382
383 ##########################   MAIN TRANSLATOR SUBROUTINE   #####################
384 sub basic_lyx {
385 # This subroutine is called by Text::TeX::process each time subroutine
386 #     eat returns a value.
387 # Argument 0 is the return value from subroutine eat
388 # Argument 1 is the Text::TeX::OpenFile (namely, $TeXfile)
389     my $eaten = shift;
390     my $fileobject = shift;
391
392     # This handles most but maybe not all comments
393     # THere shouldn't be any if we've run CleanTeX.pl
394     print "Comment: ",$eaten->comment if defined $eaten->comment && $debug_on;
395
396     my $type = ref($eaten);
397     print "$type " if $debug_on;
398
399     # This loop is basically just a switch. However, making it a for
400     #    (1) makes $_ = $type (convenient)
401     #    (2) allows us to use things like next and last
402     TYPESW: for ($type) {
403
404         # some pre-case work
405         s/^Text::TeX:://o or die "unknown token?!";
406         my ($dummy, $tok);
407         my ($thistable);
408
409         # The parser puts whitespace before certain kinds of tokens along
410         # with that token. If that happens, save a space
411         my $pre_space = ""; # whitespace before a token
412         if (/BegArgsToken|^Token|::Group$/) {
413             $dummy = $eaten->exact_print;
414             # Only set prespace if we match something
415             #    We wouldn't want it to be more than one space, since that's
416             # illegal in LyX. Also, replace \t or \n with ' ' since they are
417             # ignored in LyX. Hopefully, this won't lead to anything worse
418             # than some lines with >80 chars
419             #    Finally, don't put a space at the beginning of a new paragraph
420             if (($dummy =~ /^\s+/) && !$IsNewParagraph) {
421                 $pre_space = " ";
422             }
423         }
424
425         # Handle blank lines.
426         if (m/^Paragraph$/o) {
427             # $INP <>0 means We will need a new layout command
428             $IsNewParagraph = 1;
429
430             # $MBD means start a begin_deeper within list environments
431             #    unless there's an \item command
432             $MayBeDeeper = 1;
433
434             last TYPESW;
435         }
436
437         # If, e.g., there's just a comment in this token, don't do anything
438         # This actually shouldn't happen if CleanTeX has already removed them
439         last TYPESW if !defined $eaten->print;
440         
441         # Handle LaTeX tokens
442         if (/^Token$/o) {
443
444             my $name = $eaten->token_name; # name of the token, e.g., "\large"
445             print "'$name' " if $debug_on;
446
447             # Tokens which turn into a bit of LyX text
448             if (exists $TextTokenTransTable{$name}) {
449                 &CheckForNewParagraph; #Start new paragraph if necessary
450
451                 my $to_print = $TextTokenTransTable{$name};
452
453                 # \@ has to be specially handled, because it depends on
454                 # the character AFTER the \@
455                 if ($name eq '\@') {
456                     my $next = $fileobject->eatGroup(1);
457                     my $ch="";
458                     $ch = $next->print or warn "\@ confused me!\n";
459                     if ($ch eq '.') {
460                         # Note: \@ CAN'T have pre_space before it
461                         print OUTFILE "$to_print$ch\n";
462                         print "followed by $ch" if $debug_on;
463                     } else {
464                        warn "LyX (or LaTeX) can't handle '$ch' after $name\n";
465                         print OUTFILE $ch;
466                     }
467
468                 } else { # it's not \@
469                     # Print the translated text (include preceding whitespace)
470                     print OUTFILE "$pre_space$to_print";
471                 } # end special handling for \@
472
473             # Handle tokens that LyX translates as a "LatexCommand" inset
474             } elsif (foundIn($name, @LatexCommands) ||
475                      isNatbibCitation($name)){
476                 &CheckForNewParagraph; #Start new paragraph if necessary
477                 print OUTFILE "$pre_space\n\\begin_inset LatexCommand ",
478                                $name,
479                               "\n\n\\end_inset \n\n";
480
481             # Math -- copy verbatim until you're done
482             } elsif (starting_math($name)) {
483                 print "\nCopying math beginning with '$name'\n" if $debug_on;
484                 # copy everything until end text
485                 $dummy = &Verbatim::copy_verbatim($fileobject, $eaten);
486                 $dummy = &fixmath($dummy); # convert '\sp' to '^', etc.
487
488                 &CheckForNewParagraph; # math could be first thing in a par
489                 print OUTFILE "$pre_space\n\\begin_inset Formula $name ";
490                 print $dummy if $debug_on;
491                 print OUTFILE $dummy;
492
493             } elsif (ending_math($name)) {
494                 # end math
495                 print OUTFILE "$name\n\\end_inset \n\n";
496                 print "\nDone copying math ending with '$name'" if $debug_on;
497
498             # Items in list environments
499             } elsif ($name eq '\item') {
500                 
501                 # What if we had a nested "Standard" paragraph?
502                 # Then write \end_deeper to finish the standard layout
503                 #     before we write the new \layout ListEnv command
504                 if ($$CurrentLayoutStack[-1] eq "Standard") {
505                     pop (@$CurrentLayoutStack); # take "Standard" off the stack
506                     print OUTFILE "\n\\end_deeper ";
507                     print "\nCurrent Layout Stack: @$CurrentLayoutStack"
508                           if $debug_on;
509                 } # end deeper if
510
511                 # Upcoming text (the item) will be a new paragraph, 
512                 #    requiring a new layout command based on whichever
513                 #    kind of list environment we're in
514                 $IsNewParagraph = 1;
515
516                 # But since we had an \item command, DON'T nest a
517                 #    deeper "Standard" paragraph in the list
518                 $MayBeDeeper = 0;
519
520                 # Check for an optional argument to \item
521                 # If so, within the [] we need to protect spaces
522                 # TODO: In fact, for description, if there's no [] or
523                 # there's an empty [], then we need to write a ~, since LyX
524                 # will otherwise make the next word the label
525                 # If it's NOT a description & has a [] then we're stuck!
526                 # They need to fix the bullets w/in lyx!
527                 if (($dummy = $fileobject->lookAheadToken) &&
528                     ($dummy =~ /\s*\[/)) {
529                     $fileobject->eatFixedString('\['); # eat the [
530                     $protect_spaces = 1;
531                 }
532
533                 # Special lists (e.g. List layout) have to print something
534                 # before each item. In that case, CFNP and print it
535                 if ($item_preface) {
536                     &CheckForNewParagraph;
537                     print OUTFILE $item_preface;
538                 }
539
540             # Font sizing commands
541             # (Other font commands are TT::BegArgsTokens because they take
542             #     arguments. Font sizing commands are 'local' TT::Tokens)
543             } elsif (exists $FontTransTable{$name}) {
544                 my $command = $FontTransTable{$name}; #e.g., '\size large'
545
546                 if (! $IsNewParagraph) {
547                     print OUTFILE "$pre_space$command";
548                 } #otherwise, wait until we've printed the new paragraph command
549
550                 # Store the current font change
551                 ($dummy = $command) =~ s/\s*(\S+)\s+(\w+)\s*/$1/;
552                 die "Font command error" if !exists $$CurrentFontStatus{$dummy};
553                 push (@{$CurrentFontStatus->{$dummy}}, $2);
554                 print "\nCurrent $dummy Stack: @{$CurrentFontStatus->{$dummy}}"
555                       if $debug_on;
556
557             # Table stuff
558             } elsif ($name eq '&') {
559                 if ($thistable = &RelyxTable::in_table) {
560                     print OUTFILE "\n\\newline \n";
561                     $thistable->nextcol;
562                 } else {warn "& is illegal outside a table!"}
563
564             } elsif ($name eq '\\\\' || $name eq '\\newline' || $name eq "\\tabularnewline") {
565                 &CheckForNewParagraph; # could be at beginning of par?
566                 print OUTFILE "\n\\newline \n";
567
568                 # If we're in a table, \\ means add a row to the table
569                 # Note: if we're on the last row of the table, this extra
570                 # row will get deleted later. This hack is necessary, because
571                 # we don't know while reading when the last row is!
572                 if ($thistable = &RelyxTable::in_table) {
573                     $thistable->addrow;
574                 }
575
576             } elsif ($name eq '\hline') {
577                 if ($thistable = &RelyxTable::in_table) {
578                     # hcline does hline if no arg is given
579                     $thistable->hcline;
580                 } else {warn "\\hline is illegal outside a table!"}
581
582             # Figures
583
584             } elsif ($name =~ /^\\epsf[xy]size$/) {
585                 # We need to eat '=' followed by EITHER more text OR
586                 # one (or more?!) macros describing a TeX size
587                 my $arg = $fileobject->eatMultiToken;
588                 my $length = $arg->print;
589                 $length =~ s/^\s*=\s*// or warn "weird '$name' command!";
590
591                 # If there's no "cm" or other letters in $length, the next token
592                 # ought to be something like \textwidth. Then it will be empty
593                 # or just have numbers in it.
594                 # This is bugprone. Hopefully not too many people use epsf!
595                 if ($length =~ /^[\d.]*\s*$/) {
596                     my $next = $fileobject->eatMultiToken;
597                     $length .= $next->print;
598                 }
599                 $length =~ s/\s*$//; # may have \n at end
600
601                 # If we can't parse the command, print it in tex mode
602                 &RelyxFigure::parse_epsfsize($name, $length) or 
603                     &print_tex_mode("$name=$length");
604
605             # Miscellaneous...
606
607             } elsif ($name =~ /\\verb.*?/) {
608                 my $dummy = &Verbatim::copy_verb($fileobject, $eaten);
609                 print "\nCopying $name in TeX mode: " if $debug_on;
610                 &print_tex_mode ($dummy);
611
612             # Otherwise it's an unknown token, which must be copied
613             #     in TeX mode, along with its arguments, if any
614             } else {
615                 if (defined($eaten->relyx_args($fileobject))) {
616                    &copy_latex_known($eaten, $fileobject);
617                 } else { # it's not in MyTokens
618                     &copy_latex_unknown($eaten, $fileobject);
619                 }
620             }
621
622             last TYPESW;
623         }
624         
625         # Handle tokens that take arguments, like \section{},\section*{}
626         if (/^BegArgsToken$/) {
627             my $name = $eaten->token_name;
628             print "$name" if $debug_on;
629
630             # Handle things that LyX translates as a "LatexCommand" inset
631             if (foundIn($name, @LatexCommands) || isNatbibCitation($name)){
632                 &CheckForNewParagraph; #Start new paragraph if necessary
633
634                 print OUTFILE "$pre_space\n\\begin_inset LatexCommand ";
635
636                 #    \bibliography gets handled as a LatexCommand inset, but
637                 # it's a special case, cuz LyX syntax expects "\BibTeX"
638                 # instead of "\bibliography" (I have no idea why), and because
639                 # we have to print extra stuff
640                 #    Because we might not have encountered the
641                 # \bibliographystyle command yet, we write
642                 # "insert bibstyle here", and replace that string
643                 # with the actual bibliographystyle argument in
644                 # LastLyX (i.e., the next pass through the file)
645                 if ($name eq "\\bibliography") {
646                     print OUTFILE "\\BibTeX[", $bibstyle_insert_string, "]";
647                 } else {
648                     print OUTFILE "$name";
649                 }
650
651                 # \cite takes an optional argument, e.g.
652                 my $args = $eaten->relyx_args ($fileobject);
653                 while ($args =~ s/^o//) {
654                     my $tok = $fileobject->eatOptionalArgument;
655                     my $dummy = $tok->exact_print;
656                     print OUTFILE $dummy;
657                 }
658
659                 print OUTFILE "\{";
660                 last TYPESW; # skip to the end of the switch
661             }
662
663             if (grep {$_ eq $name} @IncludeCommands) {
664                 &CheckForNewParagraph; #Start new paragraph if necessary
665                 print OUTFILE "$pre_space\n$Begin_Inset_Include $name\{";
666                 last TYPESW; # skip to the end of the switch
667             }
668
669             # This is to handle cases where _ is used, say, in a filename.
670             # When _ is used in math mode, it'll be copied by the math mode
671             # copying subs. Here we handle cases where it's used in non-math.
672             # Examples are filenames for \include & citation labels.
673             # (It's illegal to use it in regular LaTeX text.)
674             if ($name eq "_") {
675                print OUTFILE $eaten->exact_print;
676                last TYPESW;
677             }
678
679             # Sectioning and Title environments (using a LyX \layout command)
680             if (exists $ReadCommands::ToLayout->{$name}) {
681                 &ConvertToLayout($name);
682                 last TYPESW; #done translating
683
684             # Font characteristics
685             } elsif (exists $FontTransTable{$name}) {
686                 my $dum2;
687                 my $command = $FontTransTable{$name};
688                 ($dummy, $dum2) = ($command =~ /(\S+)\s+(\w+)/);
689
690                 # HACK so that "\emph{hi \emph{bye}}" yields unemph'ed "bye"
691                 if ( ($dummy eq "\\emph") && 
692                      ($CurrentFontStatus->{$dummy}->[-1] eq "on")) {
693                        $dum2 = "default"; # "on" instead of default?
694                        $command =~ s/on/default/;
695                 }
696
697                 # If we're about to start a new paragraph, save writing
698                 #    this command until *after* we write '\layout Standard'
699                 if (! $IsNewParagraph) {
700                     print OUTFILE "$pre_space$command";
701                 }
702
703                 # Store the current font change
704                 die "Font command error" if !exists $$CurrentFontStatus{$dummy};
705                 push (@{$CurrentFontStatus->{$dummy}}, $dum2);
706
707
708             # Handle footnotes and margin notes
709             # Make a new font table & layout stack which will be local to the 
710             #    footnote or marginpar
711             } elsif (exists $FloatTransTable{$name}) {
712                 my $command = $FloatTransTable{$name};
713
714                 # Open the footnote
715                 print OUTFILE "$pre_space$command";
716
717                 # Make $CurrentFontStatus point to a new (anonymous) font table
718                 $CurrentFontStatus =  {
719                     '\emph' => ["default"],
720                     '\family' => ["default"],
721                     '\series' => ["default"],
722                     '\shape' => ["default"],
723                     '\bar' => ["default"],
724                     '\size' => ["default"],
725                     '\noun' => ["default"],
726                 };
727
728                 # And make $CurrentLayoutStack point to a new (anon.) stack
729                 $CurrentLayoutStack = ["Standard"];
730
731                 # Store whether we're at the end of a paragraph or not
732                 #    for when we get to end of footnote AND 
733                 # Note that the footnote text will be starting a new paragraph
734                 # Also store the current alignment (justification)
735                 $OldINP = $IsNewParagraph; $OldMBD = $MayBeDeeper;
736                 $OldAlignment = $CurrentAlignment;
737                 $IsNewParagraph = 1;
738                 $MayBeDeeper = 0; #can't be deeper at beginning of footnote
739                 $CurrentAlignment = "";
740
741             # Accents
742             } elsif ($name =~ m/^$AccentTokens$/) {
743                 &CheckForNewParagraph; # may be at the beginning of a par
744
745                 print OUTFILE "$pre_space\n",'\i ',$name,'{'
746             
747             # Included Postscript Figures
748             # Currently, all postscript including commands take one
749             # required argument and 0 to 2 optional args, so we can
750             # clump them together in one else.
751             } elsif (grep {$_ eq $name} @GraphicsCommands) {
752                 &CheckForNewParagraph; # may be at the beginning of a par
753                 my $arg1 = $fileobject->eatOptionalArgument;
754                 # arg2 is a token of an empty string for most commands
755                 my $arg2 = $fileobject->eatOptionalArgument;
756                 my $arg3 = $fileobject->eatRequiredArgument;
757                 my $save = $arg1->exact_print . $arg2->exact_print .
758                            $arg3->exact_print;
759
760                 # Parse and put figure into LyX file
761                 # Print it verbatim if we didn't parse correctly
762                 my $thisfig = new RelyxFigure::Figure;
763                 if ($thisfig->parse_pscommand($name, $arg1, $arg2, $arg3)) {
764                     print OUTFILE $thisfig->print_info;
765                 } else {
766                     &print_tex_mode($eaten->exact_print . $save);
767                 }
768
769             # Tables
770
771             } elsif ($name eq "\\multicolumn") {
772                 if ($thistable = &RelyxTable::in_table) {
773                     # the (simple text) first arg.
774                     $dummy = $fileobject->eatRequiredArgument->contents->print;
775                     my $group = $fileobject->eatRequiredArgument;
776                     $thistable->multicolumn($dummy, $group);
777                 } else {warn "\\multicolumn is illegal outside a table!"}
778
779             } elsif ($name eq '\cline') {
780                 if ($thistable = &RelyxTable::in_table) {
781                     # the (simple text) first arg.
782                     $dummy = $fileobject->eatRequiredArgument->contents->print;
783                     # sub hcline does cline if an arg is given
784                     $thistable->hcline($dummy);
785                 } else {warn "\\cline is illegal outside a table!"}
786
787             # Bibliography
788
789             } elsif ($name eq '\bibliographystyle') {
790                 $tok = $fileobject->eatRequiredArgument;
791                 $bibstyle_file = "";
792                 # There may be >1 token in the {}, e.g. "{a_b}" -> 3 tokens
793                 my @toks = $tok->contents;
794                 foreach $tok (@toks) {
795                     # kludge: CleanTeX adds {} after _
796                     $tok = $tok->contents if ref($tok) eq "Text::TeX::Group";
797                     $bibstyle_file .= $tok->print;
798                 }
799                 print "\nBibliography style file is $bibstyle_file"if $debug_on;
800
801             # LyX \bibitem actually looks just like LaTeX bibitem, except
802             # it's in a Bibliography par & there must be a space after the
803             # bibitem command. Note we need to explicitly print the braces...
804             } elsif ($name eq "\\bibitem") {
805                 $IsNewParagraph=1; # \bibitem always starts new par. in LyX
806                 &CheckForNewParagraph;
807
808                 $tok = $fileobject->eatOptionalArgument;
809                 print OUTFILE "$name ", $tok->exact_print, "{";
810
811             # Miscellaneous
812
813             # ensuremath -- copy verbatim until you're done
814             # but add \( and \)
815             # Note that we'll only get here if the command is NOT in math mode
816             } elsif ($name eq '\ensuremath') {
817                 print "\nCopying math beginning with '$name'\n" if $debug_on;
818                 my $tok = $fileobject->eatGroup; # eat math expression
819                 my $dummy = $tok->exact_print;
820                 $dummy =~ s/\{(.*)\}/$1/;
821                 $dummy = &fixmath($dummy); # convert '\sp' to '^', etc.
822
823                 &CheckForNewParagraph; # math could be first thing in a par
824                 print OUTFILE "$pre_space\n\\begin_inset Formula \\( ";
825                 print $dummy if $debug_on;
826                 print OUTFILE $dummy;
827
828                 # end math
829                 print OUTFILE "\\)\n\\end_inset \n\n";
830                 print "\nDone copying math" if $debug_on;
831
832             # Token in the ReadCommands command list that basic_lyx doesn't
833             #    know how to handle
834             } else {
835                 &copy_latex_known($eaten,$fileobject);
836             } # end big if
837
838             # Exit the switch
839             last TYPESW;
840         }
841
842         # ArgTokens appear when we've used eatRequiredArgument
843         if (/^ArgToken$/) {
844             # If we're copying a recognized but untranslatable token in tex mode
845             my $tok = $tex_mode_tokens[-1] || 0;
846             if ($eaten->base_token == $tok) {
847                 &copy_latex_known($eaten,$fileobject);
848             }
849         
850             last TYPESW;
851         }
852
853         if (/^EndArgsToken$/) {
854             # If we're copying a recognized but untranslatable token in tex mode
855             my $tok = $tex_mode_tokens[-1] || 0;
856             if ($eaten->base_token eq $tok) {
857                 &copy_latex_known($eaten,$fileobject);
858                 last TYPESW;
859             }
860
861             my $name = $eaten->token_name;
862             print "$name" if $debug_on;
863
864             # Handle things that LyX translates as a "LatexCommand" inset
865             # or "Include" insets
866             if (foundIn($name, @LatexCommands, @IncludeCommands) ||
867                 isNatbibCitation($name)){
868                 print OUTFILE "\}\n\n\\end_inset \n\n";
869
870             } elsif (exists $ReadCommands::ToLayout->{$name}) {
871                 &EndLayout($name);
872
873             # Font characteristics
874             # Pop the current FontStatus stack for a given characteristic
875             #    and give the new command (e.g., \emph default)
876             } elsif (exists $FontTransTable{$name}) {
877                 my $command = $FontTransTable{$name};
878                 ($dummy) = ($command =~ /(\S+)\s+\w+/);
879                 pop @{$CurrentFontStatus->{$dummy}};
880                 $command = "\n$dummy $CurrentFontStatus->{$dummy}->[-1] \n";
881                 print OUTFILE "$command";
882
883             # Footnotes and marginpars
884             } elsif (exists $FloatTransTable{$name}) {
885                 print OUTFILE "\n\\end_float \n\n";
886
887                 # Reset the layout stack and font status table pointers to
888                 #    point to the global stack/table again, instead of the
889                 #    footnote-specific stack/table
890                 $CurrentFontStatus = \%FontStatus;
891                 $CurrentLayoutStack = \@LayoutStack;
892
893                 # We need to reissue any font commands (but not layouts)
894                 foreach $dummy (keys %$CurrentFontStatus) {
895                     if ($CurrentFontStatus->{$dummy}->[-1] ne "default") {
896                         print OUTFILE $FontTransTable{$dummy};
897                     }
898                 }
899
900                 # Same paragraph status as we had before the footnote started
901                 $IsNewParagraph = $OldINP; $MayBeDeeper = $OldMBD;
902                 $CurrentAlignment = $OldAlignment;
903
904             } elsif ($name =~ m/^$AccentTokens$/) {
905                 print OUTFILE "}\n";
906             
907             } elsif ($name eq "\\bibitem") {
908                 print OUTFILE "}\n";
909             } # End if on $name
910
911             # Exit main switch
912             last TYPESW;
913         } # end if EndArgsToken
914
915         # Handle END of scope of local commands like \large
916         if (/^EndLocal$/) {
917             my $name = $eaten->token_name; #cmd we're ending, e.g.,\large
918             print $name if $debug_on;
919
920             if (exists $FontTransTable{$name}) {
921                 my $command = $FontTransTable{$name};
922                 ($dummy = $command) =~ s/\s*(\S*)\s+(\w+)\s*/$1/; #e.g., '\size'
923                 die "Font command error" if !exists $$CurrentFontStatus{$dummy};
924                 # TT::OF->check_presynthetic returns local commands FIFO!
925                 # So pop font stack, but warn if we pop the wrong thing
926                 warn " font confusion?" if
927                            pop @{$CurrentFontStatus->{$dummy}} ne $2;
928                 print "\nCurrent $dummy Stack: @{$CurrentFontStatus->{$dummy}}"
929                       if $debug_on;
930                 my $newfont = $CurrentFontStatus->{$dummy}->[-1];
931                 $command = "\n$dummy $newfont\n";
932                 print OUTFILE "$command";
933
934             } else {
935                 warn "Unknown EndLocal token!\n";
936             }
937
938             last TYPESW;
939         }
940
941         # We don't print { or }, but we make sure that the spacing is correct
942         # Handle '{'
943         if (/^Begin::Group$/) {
944             print OUTFILE "$pre_space";
945             last TYPESW;
946         }
947
948         # Handle '{'
949         if (/^End::Group$/) {
950             print OUTFILE "$pre_space";
951             last TYPESW;
952         }
953
954         # Handle \begin{foo}
955         if (/^Begin::Group::Args$/) {
956             print $eaten->print," " if $debug_on; # the string "\begin{foo}"
957             my $env = $eaten->environment;
958             
959             # Any environment found in the layouts files
960             if (exists $ReadCommands::ToLayout->{$env}) {
961                 &ConvertToLayout($env);
962
963                 # Some environments have an extra argument. In that case,
964                 # print the \layout command (cuz these environments always
965                 # start new pars). Then either print the extra arg or
966                 # ignore it (depending on the environment).
967                 if (exists $ExtraArgEnvironments{$env}) {
968                     # Should be just one token in the arg.
969                     my $arg = $fileobject->eatBalanced->contents->print;
970
971                     if ($ExtraArgEnvironments{$env}) { #print it
972                         print "\nArgument $arg to $env environment"
973                                                                 if $debug_on;
974                         $item_preface = $ExtraArgEnvironments{$env} . $arg."\n";
975
976                     } else { #ignore it
977                         print "\nIgnoring argument '$arg' to $env environment"
978                                                                 if $debug_on;
979                     }
980                 } # end if for reading extra args to \begin command
981
982             # Math environments
983             } elsif ($env =~ /^$MathEnvironments$/o) {
984                 &CheckForNewParagraph; # may be beginning of paragraph
985                 my $begin_text = $eaten->print;
986                 print "\nCopying math beginning with '$begin_text'\n"
987                                                               if $debug_on;
988                 print OUTFILE "\n\\begin_inset Formula $begin_text ";
989                 $dummy = &Verbatim::copy_verbatim($fileobject, $eaten);
990                 $dummy = &fixmath($dummy); # convert '\sp' to '^', etc.
991                 print $dummy if $debug_on;
992                 print OUTFILE $dummy;
993
994             # Alignment environments
995             } elsif (exists $AlignEnvironments{$env}) {
996                 # Set it to the command which creates this alignment
997                 $CurrentAlignment = $AlignEnvironments{$env};
998                 ($dummy) = ($CurrentAlignment =~ /\S+\s+(\w+)/);
999                 print "\nNow $dummy-aligning text " if $debug_on;
1000
1001                 # alignment environments automatically start a new paragraph
1002                 $IsNewParagraph = 1;
1003
1004             # Environments lyx translates to floats
1005             } elsif (exists $FloatEnvTransTable{$env}) {
1006                 # this really only matters if it's at the very
1007                 # beginning of the doc.
1008                 &CheckForNewParagraph;
1009
1010                 $tok = $fileobject->eatOptionalArgument;
1011                 if ($tok && defined ($dummy = $tok->print) && $dummy) {
1012                     print "\nIgnoring float placement '$dummy'" if $debug_on;
1013                 }
1014                 my $command = $FloatEnvTransTable{$env};
1015
1016                 # Open the table/figure
1017                 print OUTFILE "$command";
1018
1019             # table
1020             } elsif ($env =~ /^tabular$/) { # don't allow tabular* or ctabular
1021                 # Table must start a new paragraph
1022                 $IsNewParagraph = 1; $MayBeDeeper = 1;
1023                 # We want to print table stuff *after* a \layout Standard
1024                 &CheckForNewParagraph;
1025
1026                 # Since table info needs to come *before* the table content,
1027                 #    put a line in the output file saying that the *next*
1028                 #    reLyX pass needs to put the table info there
1029                 print OUTFILE "\n$RelyxTable::TableBeginString\n";
1030
1031                 # Read and ignore an optional argument [t] or [b]
1032                 $tok = $fileobject->eatOptionalArgument;
1033                 if ($tok && defined ($dummy = $tok->print) && $dummy) {
1034                     print "\nIgnoring positioning arg '$dummy'" if $debug_on;
1035                 }
1036
1037                 # Read the argument into a TT::Group
1038                 #   (that group may contain groups, e.g. for {clp{10cm}}
1039                 $tok = $fileobject->eatGroup;
1040                 new RelyxTable::Table $tok;
1041
1042             # minipage
1043             } elsif ($env eq "minipage") {
1044                 &CheckForNewParagraph;
1045
1046                 print OUTFILE "\\begin_inset Minipage\n";
1047
1048                 # The minipage environment is defined as:
1049                 # \begin{minipage}[pos][height][inner-pos]{width} <text>
1050                 # \end{minipage}
1051
1052                 # Read the position optional argument, if it exists
1053                 $tok = $fileobject->eatOptionalArgument;
1054                 my $pos = $tok->print if defined($tok->print);
1055
1056                 my %map = ('t' => '0', 'c' => '1', 'b' => '2');
1057                 if ($debug_on && $pos ne '' && !defined($map{$pos})) {
1058                     print "\nIgnoring unknown positioning arg '$pos'\n";
1059                 }
1060
1061                 # The minipage is centred by default.
1062                 $pos = '1' if (!defined($map{$pos}) ||
1063                                   ($pos = $map{$pos}) eq '');
1064
1065                 # Read the height optional argument, if it exists
1066                 my $height = '0pt';
1067                 $tok = $fileobject->eatOptionalArgument;
1068                 if (defined($tok->print)) {
1069                     $height = getAsLyXLength($tok->print);
1070                 }
1071
1072                 # Read the inner-pos optional argument, if it exists
1073                 my $innerpos = $pos;
1074                 $tok = $fileobject->eatOptionalArgument;
1075                 if (defined($tok->print)) {
1076                     my $arg = $tok->print;
1077                     print("\nMinipage inner-pos argument, $arg, is ",
1078                           "currently ignored\n");
1079                 }
1080
1081                 # Read the width as (a reference to) an array of tokens.
1082                 $tok = $fileobject->eatBalanced;
1083                 my $width = getAsLyXLength($tok->exact_print);
1084
1085                 print OUTFILE "position $pos\n";
1086                 print OUTFILE "inner_position $innerpos\n";
1087                 print OUTFILE "height \"$height\"\n";
1088                 print OUTFILE "width \"$width\"\n";
1089                 print OUTFILE "collapsed false\n";
1090
1091             # \begin document
1092             } elsif ($env eq "document") {
1093                 # do nothing
1094                 #print "\nStarting to translate actual document" if $debug_on;
1095
1096             # Special environments to copy as regular text (-r option).
1097             # Do this by copying the \begin & \end command in TeX mode
1098             # (\Q\E around $env allows *'s in environment names!)
1099             } elsif (grep /^\Q$env\E$/, @ReadCommands::regular_env) {
1100                 print "\nCopying $env environment as regular text\n"
1101                                                               if $debug_on;
1102                 $dummy = $eaten->print; # \begin{env}, ignore initial whitespace
1103                 &print_tex_mode($dummy);
1104
1105             # otherwise, it's an unknown environment
1106             # In that case, copy everything up to the \end{env}
1107             #    Save stuff in global tex_mode_string so we can print it
1108             # when we read & handle the \end{env}
1109             } else {
1110
1111                 print "\nUnknown environment $env" if $debug_on;
1112                 $tex_mode_string = "";
1113                 # print "\begin{env}
1114                 # For reLyXskip env, don't print the \begin & \end commands!
1115                 $tex_mode_string .= $eaten->exact_print 
1116                                 unless $env eq "reLyXskip";
1117                 $tex_mode_string .=&Verbatim::copy_verbatim($fileobject,$eaten);
1118             }
1119
1120             last TYPESW;
1121         }
1122         
1123         # Handle \end{foo}
1124         if (/^End::Group::Args$/) {
1125             print $eaten->print," " if $debug_on; # the string "\end{foo}"
1126             my $env = $eaten->environment;
1127
1128             # End of list or quote/verse environment
1129             # OR special environment given with -t option
1130             if (exists $ReadCommands::ToLayout->{$env}) {
1131                 &EndLayout($env);
1132                 $item_preface = ""; # reset when at end of List env.
1133
1134             # End of math environments
1135             } elsif ($env =~ /^$MathEnvironments$/o) {
1136                 print OUTFILE "\\end{$env}\n\\end_inset \n\n";
1137                 print "\nDone copying math environment '$env'" if $debug_on;
1138
1139             } elsif (exists $AlignEnvironments{$env}) {
1140                 # Back to block alignment
1141                 $CurrentAlignment = "";
1142                 print "\nBack to block alignment" if $debug_on;
1143
1144                 # assume that \end should end a paragraph
1145                 # This isn't correct LaTeX, but LyX can't align part of a par
1146                 $IsNewParagraph = 1;
1147
1148             # Environments lyx translates to floats
1149             } elsif (exists $FloatEnvTransTable{$env}) {
1150                 print OUTFILE "\n\\end_float \n\n";
1151
1152             # table
1153             } elsif ($env =~ /tabular$/) { # don't allow tabular*
1154                 if ($thistable = &RelyxTable::in_table) {
1155                     $thistable->done_reading;
1156                     print OUTFILE "\n$RelyxTable::TableEndString\n";
1157                 } else {warn "found \\end{tabular} when not in table!"}
1158
1159                 # Anything after a table will be a new paragraph
1160                 $IsNewParagraph = 1; $MayBeDeeper = 1;
1161
1162             # minipage
1163             } elsif ($env eq "minipage") {
1164                 print OUTFILE "\n\\end_inset \n\n";
1165
1166                 # Next stuff will be new env.
1167                 # $IsNewParagraph = 1;
1168
1169             } elsif ($env eq "document") {
1170                 print "\nDone with document!" if $debug_on;
1171
1172             # "regular" environment given with -r option
1173             } elsif (grep /^\Q$env\E$/, @ReadCommands::regular_env) {
1174                 $dummy = $eaten->print; # \end{env}, ignore initial whitespace
1175                 &print_tex_mode($dummy);
1176
1177                 # Next stuff will be new env.
1178                 $IsNewParagraph = 1;
1179
1180             # End of unknown environments. We're already in TeX mode
1181             } else {
1182                 # Add \end{env} (including initial whitespace) to string
1183                 # For reLyXskip environment, don't print \begin & \end commands!
1184                 $tex_mode_string .= $eaten->exact_print
1185                                        unless $env eq "reLyXskip";
1186                 # Now print it
1187                 &print_tex_mode($tex_mode_string);
1188                 print "Done copying unknown environment '$env'" if $debug_on;
1189             }
1190
1191             last TYPESW;
1192
1193         }
1194
1195         # Note for text handling: we have to do lots of stuff to handle
1196         # spaces in (as close as possible to) the same way that LaTeX does
1197         #    LaTeX considers all whitespace to be the same, so basically, we
1198         # convert each clump of whitespace to one space. Unfortunately, there
1199         # are special cases, like whitespace at the beginning/end of a par,
1200         # which we have to get rid of to avoid extra spaces in the LyX display.
1201         #    \n at the end of a paragraph must be considered like a space,
1202         # because the next line might begin with a token like \LyX. But
1203         # if the next line starts with \begin, say, then an extra space will be
1204         # generated in the LyX file. Oh well. It doesn't affect the dvi file.
1205         if (/^Text$/) {
1206             my $outstr = $eaten->print; # the actual text
1207
1208             # don't bother printing whitespace
1209             #    Note: this avoids the problem of extra whitespace generating
1210             # extra Text::TeX::Paragraphs, which would generate extra
1211             # \layout commands
1212             last TYPESW if $outstr =~ /^\s+$/;
1213
1214             # whitespace at beginning of a paragraph is meaningless
1215             # e.g. \begin{foo}\n hello \end{foo} shouldn't print the \n
1216             # (Note: check IsNewParagraph BEFORE calling &CFNP, which zeros it)
1217             my $replace = $IsNewParagraph ? "" : " ";
1218             $outstr =~ s/^\s+/$replace/;
1219
1220             # Only write '\layout Standard' once per paragraph
1221             &CheckForNewParagraph;
1222
1223             # \n\n signals end of paragraph, so get rid of it (and any space
1224             # before it)
1225             $outstr =~ s/\s*\n\n$//;
1226
1227             # Print the LaTeX text to STDOUT
1228             print "'$outstr'" if $debug_on;
1229
1230             # LyX *ignores* \n and \t, whereas LaTeX considers them just
1231             # like a space.
1232             #    Also, many spaces are equivalent to one space in LaTeX
1233             # (But LyX misleadingly displays them on screen, so get rid of them)
1234             $outstr =~ s/\s+/ /g;
1235
1236             # protect spaces in an optional argument if necessary
1237             # Put a SPACE after the argument for List, Description layouts
1238             if ($protect_spaces) {
1239                 $dummy = $TextTokenTransTable{'~'};
1240
1241                 # This will not handle brackets in braces!
1242                 if ($outstr =~ /\]/) { # protect spaces only *until* the bracket
1243                     my $tempstr = $`;
1244                     my $tempstr2 = $';
1245                     # Note that any \t's have been changed to space already
1246                     $tempstr =~ s/ /$dummy/g;
1247
1248                     # Print 1 space after the argument (which finished with ])
1249                     # Don't print 2 (i.e. remove leading space from $tempstr2)
1250                     # don't print the bracket
1251                     $tempstr2 =~ s/^ //;
1252                     $outstr = "$tempstr $tempstr2";
1253                     $protect_spaces = 0; # Done with optional argument
1254                 } else { # protect all spaces, since we're inside brackets
1255                     $outstr =~ s/ /$dummy/g;
1256                 }
1257             } # end special stuff for protected spaces
1258
1259             # Translate any LaTeX text that requires special LyX handling
1260             foreach $dummy (keys %TextTransTable) {
1261                 $outstr =~ s/\Q$dummy\E/$TextTransTable{$dummy}/g;
1262             }
1263
1264             # "pretty-print" the string. It's not perfect, since there may
1265             # be text in the OUTFILE before $outstr, but it will keep from
1266             # having REALLY long lines.
1267             # Try to use approximately the same word-wrapping as LyX does:
1268             # - before space after a period, except at end of string
1269             # - before first space after column seventy-one
1270             # - after 80'th column
1271             while (1) {
1272                 $outstr =~ s/\. (?!$)/.\n /      or
1273                 $outstr =~ s/(.{71,79}?) /$1\n / or
1274                 $outstr =~ s/(.{80})(.)/$1\n$2/ or
1275                 last; # exit loop if string is < 79 characters
1276             }
1277
1278             # Actually print the text
1279             print OUTFILE "$outstr";
1280             last TYPESW;
1281         } # end TT::Text handling
1282
1283         # The default action - this should never happen!
1284         print("I don't know ",$eaten->print) if $debug_on;
1285
1286     } # end for ($type)
1287
1288     print "\n" if $debug_on;
1289
1290 } #end sub basic_lyx
1291
1292 #########################  TEX MODE  SUBROUTINES  #########################
1293
1294 # This subroutine copies and prints a latex token and its arguments if any.
1295 # This sub is only needed if the command was not found in the syntax file
1296 # Use exact_print to preserve, e.g., whitespace after macros
1297 sub copy_latex_unknown {
1298     my $eaten = shift;
1299     my $fileobject = shift;
1300     my $outstr = $eaten->exact_print;
1301     my ($dummy, $tok, $count);
1302
1303 # Copy the actual word. Copy while you've still got
1304 #     arguments. Assume all args must be in the same paragraph
1305 #     (There could be new paragraphs *within* args)
1306     #    We can't use copy_verbatim (unless we make it smarter) because
1307     # it would choke on nested braces
1308     print "\nUnknown token: '",$eaten->print,"': Copying in TeX mode\n"
1309                                                          if $debug_on;
1310     my $dum2;
1311     while (($dum2 = $fileobject->lookAheadToken) &&
1312            ($dum2 =~ /^[[{]$/)) {
1313         if ($dum2 eq '[') { #copy optional argument - assume it's simple
1314             $tok = $fileobject->eatOptionalArgument;
1315             $outstr .= $tok->exact_print; # also print brackets & whitespace
1316         } else {
1317             $count = 0;
1318             EAT: { #copied from eatBalanced, but allow paragraphs
1319                 die unless defined ($tok = $fileobject->eatMultiToken);
1320                 $outstr.="\n",redo EAT if ref($tok) eq "Text::TeX::Paragraph";
1321                 $dummy = $tok->exact_print;
1322                 $outstr .= $dummy;
1323                 # Sometimes, token will be '\n{', e.g.
1324                 $count++ if $dummy =~ /^\s*\{$/; # add a layer of nesting
1325                 $count-- if $dummy =~ /^\s*\}$/; # end one layer of nesting
1326                 redo EAT if $count; #don't dump out until all done nesting
1327             } #end EAT block
1328         } # end if $dummy = [{
1329
1330     } #end while
1331     # Add {} after macro if it's followed by '}'. Otherwise, {\foo}bar
1332     #     will yield \foobar when LyX creates LaTeX files
1333     $outstr.="{}" if $outstr=~/\\[a-zA-Z]+$/ && $dum2 eq '}';
1334
1335     # Print it out in TeX mode
1336     &print_tex_mode($outstr);
1337
1338     print "\nDone copying unknown token" if $debug_on;
1339 } # end sub copy_latex_unknown
1340
1341 # Copy an untranslatable latex command whose syntax we know, along with its
1342 # arguments
1343 #    The command itself, optional arguments, and untranslatable
1344 # arguments get copied in TeX mode. However, arguments which contain regular
1345 # LaTeX will get translated by reLyX. Do that by printing what you have so
1346 # far in TeX mode, leaving this subroutine, continuing with regular reLyX
1347 # translating, and then returning here when you reach the ArgToken or
1348 # EndArgsToken at the end of the translatable argument.
1349 #    We need to keep a stack of the tokens that brought us here, because
1350 # you might have nested commands (e.g., \mbox{hello \fbox{there} how are you}
1351 sub copy_latex_known {
1352     my ($eaten, $fileobject) = (shift,shift);
1353     my $type = ref($eaten);
1354     $type =~ s/^Text::TeX::// or die "unknown token?!";
1355
1356     # token itself for TT::Token, TT::BegArgsToken,
1357     # Corresponding BegArgsToken for ArgToken,EndArgsToken
1358     my $temp_start = $eaten->base_token;
1359
1360 # Initialize tex mode copying
1361     if ($type eq "BegArgsToken" or $type eq "Token") {
1362         print "\nCopying untranslatable token '",$eaten->print,
1363                                       "' in TeX mode" if $debug_on;
1364         push @tex_mode_tokens, $temp_start;
1365
1366         # initialize the string of stuff we're copying
1367         $tex_mode_string = $eaten->exact_print;
1368     } # Start tex copying?
1369
1370 # Handle arguments
1371     # This token's next arguments -- returns a string matching /o*[rR]?/
1372     my $curr_args = $eaten->next_args($fileobject);
1373
1374     if ($type eq "EndArgsToken" or $type eq "ArgToken") {
1375         # Print ending '}' for the group we just finished reading
1376         $tex_mode_string .= '}';
1377     }
1378
1379     # If there could be optional arguments next, copy them
1380     while ($curr_args =~ s/^o// && $fileobject->lookAheadToken eq '[') {
1381         my $opt = $fileobject->eatOptionalArgument;
1382         $tex_mode_string .= $opt->exact_print;
1383     }
1384     $curr_args =~ s/^o*//; # Some OptArgs may not have appeared
1385
1386     if ($type eq "BegArgsToken" or $type eq "ArgToken") {
1387         # Print beginning '{' for the group we're about to read
1388         $tex_mode_string .= '{';
1389     }
1390
1391     # Now copy the next required argument, if any
1392     # Copy it verbatim (r), or translate it as regular LaTeX (R)?
1393     if ($curr_args =~ s/^r//) {
1394         my $group = $fileobject->eatRequiredArgument;
1395         my $temp = $group->exact_print;
1396         # Remove braces. They're put in explicitly
1397         $temp =~ s/\{(.*)\}/$1/; # .* is greedy
1398         $tex_mode_string .= $temp;
1399
1400     } elsif ($curr_args =~ s/^R//) {
1401         print "\n" if $debug_on;
1402         &print_tex_mode($tex_mode_string);
1403         $tex_mode_string = "";
1404         print "\nTranslating this argument for ",$temp_start->print,
1405               " as regular LaTeX" if $debug_on;
1406
1407     } else { # anything but '' is weird
1408         warn "weird arg $curr_args to ",$temp_start->print,"\n" if $curr_args;
1409     }
1410
1411 # Finished tex mode copying
1412     if ($type eq "Token" or $type eq "EndArgsToken") {
1413
1414         # Add {} to plain tokens followed by { or }. Otherwise {\foo}bar
1415         # and \foo{bar} yield \foobar in the LaTeX files created by LyX
1416         my $dummy;
1417         if ($type eq "Token" and
1418                 $dummy=$fileobject->lookAheadToken and
1419                 $dummy =~ /[{}]/)
1420         {
1421             $tex_mode_string .= '{}';
1422         }
1423
1424         # Print out the string
1425         print "\n" if $debug_on;
1426         &print_tex_mode($tex_mode_string);
1427         $tex_mode_string = "";
1428
1429         # We're done with this token
1430         pop(@tex_mode_tokens);
1431
1432         my $i = $type eq "Token" ? "" : " and its arguments";
1433         my $j = $temp_start->print;
1434         print "\nDone copying untranslatable token '$j'$i in TeX mode"
1435                                                           if $debug_on;
1436     } # end tex copying?
1437 } # end sub copy_latex_known
1438
1439 # Print a string in LyX "TeX mode"
1440 #    The goal of this subroutine is to create a block of LyX which will be
1441 # translated exactly back to the original when LyX creates its temporary LaTeX
1442 # file prior to creating a dvi file.
1443 #    Don't print \n\n at the end of the string... instead just set the new
1444 # paragraph flag. Also, don't print whitespace at the beginning of the string.
1445 # Print nothing if it's the beginning of a paragraph, or space otherwise.
1446 # These two things avoid extra C-Enter's in the LyX file
1447 sub print_tex_mode {
1448     my $outstr = shift;
1449
1450     print "'$outstr'" if $debug_on;
1451
1452     # Handle extra \n's (& spaces) at beginning & end of string
1453     my $str_ends_par = ($outstr =~ s/\n{2,}$//);
1454     if ($IsNewParagraph) {
1455         $outstr =~ s/^\s+//;  # .e.g, $outstr follows '\begin{quote}'
1456     } else {
1457         # Any whitespace is equivalent to one space in LaTeX
1458         $outstr =~ s/^\s+/ /; # e.g. $outstr follows "\LaTeX{}" or "Hi{}"
1459     }
1460
1461     # Check whether string came right at the beginning of a new paragraph
1462     # This *might* not be necessary. Probably can't hurt.
1463     &CheckForNewParagraph;
1464
1465     # Get into TeX mode
1466     print OUTFILE "$start_tex_mode";
1467
1468     # Do TeX mode translation;
1469     $outstr =~ s/\\/\n\\backslash /g;
1470     # don't translate \n in '\n\backslash' that we just made!
1471     $outstr =~ s/\n(?!\\backslash)/\n\\newline \n/g;
1472
1473     # Print the actual token + arguments if any
1474     print OUTFILE $outstr;
1475
1476     # Get OUT of LaTeX mode (and end nesting if nec.)
1477     print OUTFILE "$end_tex_mode";
1478     $IsNewParagraph = $str_ends_par;
1479
1480     return;
1481 } # end sub print_tex_mode
1482
1483 ############################  LAYOUT  SUBROUTINES  ###########################
1484
1485 sub CheckForNewParagraph {
1486 # This subroutine makes sure we only write \layout command once per paragraph
1487 #    It's mostly necessary cuz 'Standard' layout is the default in LaTeX;
1488 #    there is no command which officially starts a standard environment
1489 # If we're in a table, new layouts aren't allowed, so just return
1490 # If $IsNewParagraph is 0, it does nothing
1491 # If $INP==1, It starts a new paragraph
1492 # If $CurrentAlignment is set, it prints the alignment command for this par.
1493 # If $MayBeDeeper==1 and we're currently within a list environment,
1494 #    it starts a "deeper" Standard paragraph
1495     my $dummy; 
1496     my $layout = $$CurrentLayoutStack[-1];
1497
1498     return if &RelyxTable::in_table;
1499
1500     if ($IsNewParagraph) {
1501         # Handle standard text within a list environment specially
1502         if ($MayBeDeeper) {
1503             if ($layout =~ /^$ListLayouts$/o) {
1504                 push (@$CurrentLayoutStack, "Standard");
1505                 print "\nCurrent Layout Stack: @$CurrentLayoutStack\n"
1506                                                      if $debug_on;
1507                 print OUTFILE "\n\\begin_deeper ";
1508                 $layout = "Standard";
1509             }
1510             $MayBeDeeper = 0; # Don't test again until new paragraph
1511         }
1512
1513         # Print layout command itself
1514         print OUTFILE "\n\\layout $layout\n\n";
1515         print OUTFILE $CurrentAlignment if $CurrentAlignment;
1516
1517         # Now that we've written the command, it's no longer a new paragraph
1518         $IsNewParagraph = 0;
1519
1520         # And we're no longer waiting to see if this paragraph is empty
1521         $PendingLayout = 0;
1522
1523         # When you write a new paragraph, reprint any font commands
1524         foreach $dummy (keys %$CurrentFontStatus) {
1525             my $currf = $CurrentFontStatus->{$dummy}->[-1];
1526             if ($currf ne "default") {
1527                 print OUTFILE "\n$dummy $currf \n";
1528             }
1529         }
1530     } # end if $INP
1531 } # end sub CheckForNewParagraph
1532
1533 sub ConvertToLayout {
1534 # This subroutine begins a new layout, pushing onto the layout stack, nesting
1535 # if necessary. It doesn't actually write the \layout command -- that's
1536 # done by CheckForNewParagraph.
1537 #    The subroutine assumes that it's being passed an environment name or macro
1538 # which is known and which creates a known layout
1539 #    It uses the ToLayout hash (created by the ReadCommands module) which
1540 # gives the LyX layout for a given LaTeX command or environment
1541 # Arg0 is the environment or macro
1542     my $name = shift;
1543
1544     my $layoutref = $ReadCommands::ToLayout->{$name};
1545     my $layout = $layoutref->{'layout'};
1546     my $keepempty = $layoutref->{'keepempty'};
1547     my $dummy = ($name =~ /^\\/ ? "macro" : "environment");
1548     print "\nChanging $dummy $name to layout $layout" if $debug_on;
1549
1550     # Nest if the layout stack has more than just "Standard" in it
1551     if ($#{$CurrentLayoutStack} > 0) {
1552         # Die here for sections & things that can't be nested!
1553         print " Nesting!" if $debug_on;
1554         print OUTFILE "\n\\begin_deeper ";
1555     }
1556
1557     # If we still haven't printed the *previous* \layout command because that
1558     # environment is empty, print it now! (This happens if an environment
1559     # is nested inside a keepempty, like slide.)
1560     &CheckForNewParagraph if $PendingLayout;
1561
1562     # Put the new layout onto the layout stack
1563     push @$CurrentLayoutStack, $layout;
1564     print "\nCurrent Layout Stack: @$CurrentLayoutStack" if $debug_on;
1565
1566     # Upcoming text will be new paragraph, needing a new layout cmd
1567     $IsNewParagraph = 1;
1568
1569     # Test for nested "Standard" paragraph in upcoming text?
1570     # Some environments can nest. Sections & Title commands can't
1571     $MayBeDeeper = $layoutref->{"nestable"};
1572
1573     # We haven't yet read any printable stuff in the new paragraph
1574     # If this is a layout that's allowed to be empty, prepare for an
1575     # empty paragraph
1576     $PendingLayout = $keepempty;
1577
1578 } # end sub ConvertToLayout
1579
1580 sub EndLayout {
1581 # This subroutine ends a layout, popping the layout stack, etc.
1582 #    The subroutine assumes that it's being passed an environment name or macro
1583 # which is known and which creates a known layout
1584 #    It uses the ToLayout hash (created by the ReadCommands module) which
1585 # gives the LyX layout for a given LaTeX command or environment
1586 # Arg0 is the environment or macro
1587     my $name = shift;
1588
1589     my $layoutref = $ReadCommands::ToLayout->{$name};
1590     my $layout = $layoutref->{'layout'};
1591     my $dummy = ($name =~ /^\\/ ? "macro" : "environment");
1592     print "\nEnding $dummy $name (layout $layout)" if $debug_on;
1593
1594     # If we still haven't printed the *previous* \layout command because that
1595     # environment is empty, print it now! Before we pop the stack!
1596     # This happens for a totally empty, non-nesting environment,
1597     # like hollywood.sty's fadein
1598     &CheckForNewParagraph if $PendingLayout;
1599
1600     my $mylayout = pop (@$CurrentLayoutStack);
1601
1602     # If a standard paragraph was the last thing in a list, then
1603     #     we need to end_deeper and then pop the actual list layout
1604     # This should only happen if the Standard layout was nested
1605     #    in a nestable layout. We don't check.
1606     if ($mylayout eq "Standard") {
1607         print OUTFILE "\n\\end_deeper ";
1608         print " End Standard Nesting!" if $debug_on;
1609         $mylayout = pop (@$CurrentLayoutStack);
1610     }
1611
1612     # The layout we popped off the stack had better match the
1613     #    environment (or macro) we're ending!
1614     if ($mylayout ne $layout) { die "Problem with Layout Stack!\n"};
1615     print "\nCurrent Layout Stack: @$CurrentLayoutStack" if $debug_on;
1616
1617     # If we're finishing a nested layout, we need to end_deeper
1618     # This should only happen if the layout was nested
1619     #    in a nestable layout. We don't check.
1620     # Note that if we're nested in a list environment and the
1621     #     NEXT paragraph is Standard, then we'll have an extra
1622     #     \end_deeper \begin_deeper in the LyX file. It's sloppy
1623     #     but it works, and LyX will get rid of it when it
1624     #     resaves the file.
1625     if ($#{$CurrentLayoutStack} > 0) {
1626         print " End Nesting!" if $debug_on;
1627         print OUTFILE "\n\\end_deeper ";
1628     }
1629
1630     # Upcoming text will be new paragraph, needing a new layout cmd
1631     $IsNewParagraph = 1;
1632
1633     # Test for nested "Standard" paragraph in upcoming text?
1634     # Some environments can nest. Sections & Title commands can't
1635     $MayBeDeeper = $layoutref->{"nestable"};
1636 } # end sub EndLayout
1637
1638 #######################  MISCELLANEOUS SUBROUTINES  ###########################
1639 sub fixmath {
1640 # Translate math commands LyX doesn't support into commands it does support
1641     my $input = shift;
1642     my $output = "";
1643
1644     while ($input =~ s/
1645             (.*?)    # non-token matter ($1)
1646             (\\      # token ($2) is a backslash followed by ...
1647                 ( ([^A-Za-z] \*?) |    # non-letter (and *) ($4) OR
1648                   ([A-Za-z]+ \*?)   )  # letters (and *) ($5)
1649             )//xs) # /x to allow whitespace/comments, /s to copy \n's
1650     { 
1651         $output .= $1;
1652         my $tok = $2;
1653         if (exists $ReadCommands::math_trans{$tok}) {
1654             $tok = $ReadCommands::math_trans{$tok};
1655             # add ' ' in case we had, e.g., \|a, which would become \Verta
1656             # Only need to do it in those special cases
1657             $tok .= ' ' if
1658                     defined $4 && $tok =~ /[A-Za-z]$/ && $input =~ /^[A-Za-z]/;
1659         }
1660         $output .= $tok;
1661     }
1662     $output .= $input; # copy what's left in $input
1663
1664     return $output;
1665 }
1666
1667 1; # return true to calling subroutine
1668