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.
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)
12 use RelyxTable; # Handle LaTeX tables
13 use RelyxFigure; # Handle LaTeX figures
14 use Verbatim; # Copy stuff verbatim
16 use vars qw($bibstyle_insert_string $bibstyle_file $Begin_Inset_Include);
17 $bibstyle_insert_string = "%%%%%Insert bibliographystyle file here!";
19 $Begin_Inset_Include = "\\begin_inset Include";
21 #################### PACKAGE-WIDE VARIABLES ###########################
22 my $debug_on; # is debugging on?
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
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;
41 # HACK to protect spaces within optional argument to \item
42 my $protect_spaces = 0;
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
47 my $OldMBD; #Save $MBD during footnotes -- this should very rarely be necessary!
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;
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
61 '\emph' => ["default"],
62 '\family' => ["default"],
63 '\series' => ["default"],
64 '\shape' => ["default"],
65 '\bar' => ["default"],
66 '\size' => ["default"],
67 '\noun' => ["default"],
69 my $CurrentFontStatus = \%FontStatus;
71 # Currently aligning paragraphs right, left, or center?
72 my $CurrentAlignment = "";
73 my $OldAlignment; # Save $AS during footnotes
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
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";
83 # String to write before each item
84 my $item_preface = "";
86 ############# INFORMATION ABOUT LATEX AND LYX #############################
87 # LyX translations of LaTeX font commands
88 my %FontTransTable = (
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
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",
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",
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",
134 # Simple LaTeX tokens which are turned into small pieces of LyX text
135 my %TextTokenTransTable = (
136 # LaTeX escaped characters
146 # \i and \j are used in accents. LyX doesn't recognize them in plain
147 # text. Hopefully, this isn't a problem.
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",
158 '\LaTeXe' => "LaTeX2e",
161 '\lyxarrow' => "\\SpecialChar \\menuseparator\n",
162 '\hfill' => "\n\\hfill \n",
163 '\noindent' => "\n\\noindent \n",
164 '\textbackslash' => "\n\\backslash \n",
165 '\textgreater' => ">",
168 '\textasciitilde' => "~",
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 = (
175 "``" => "\n\\begin_inset Quotes eld\n\\end_inset \n\n",
176 "''" => "\n\\begin_inset Quotes erd\n\\end_inset \n\n",
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",
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);
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\"]";
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",
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
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";
221 # passed a string and an array
222 # returns true if the string is an element of the array.
225 return grep {$_ eq $name} @_;
228 my @NatbibCommands = map {"\\$_"} qw(citet citealt citep citealp citeauthor);
231 # returns true if it is a valid natbib citation
232 sub isNatbibCitation {
235 # These two have a single form
236 return 1 if ($name eq '\citeyear' or $name eq '\citeyearpar');
238 # Natbib citations can start with a 'C' or a 'c'
239 $name =~ s/^\\C/\\c/;
240 # The can end with a '*'
242 # Is this doctored string found in the list of valid commands?
243 return foundIn($name, @NatbibCommands);
247 ##################### PARSER INVOCATION ##################################
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)
255 my ($InFileName, $OutFileName) = (shift,shift);
257 # Before anything else, set the package-wide variables based on the
259 # opt_d is set to 1 if '-d' option given, else (probably) undefined
260 $debug_on = (defined($main::opt_d) && $main::opt_d);
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).
268 '{' => $Text::TeX::Tokens{'{'},
269 '}' => $Text::TeX::Tokens{'}'},
270 '\begin' => $Text::TeX::Tokens{'\begin'},
271 '\end' => $Text::TeX::Tokens{'\end'},
273 # Lots of other commands will be made by ReadCommands:Merge
274 # by reading the LaTeX syntax file
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'},
289 # Tokens to ignore (which make a new paragraph)
290 # Just pretend they actually ARE new paragraph markers!
291 '\maketitle' => {'class' => 'Text::TeX::Paragraph'},
294 # Now add to MyTokens all of the commands that were read from the
295 # commands file by package ReadCommands
296 &ReadCommands::Merge(\%MyTokens);
298 # Here's the actual subroutine. The above is all preparation
300 my $zzz = $debug_on ? " ($InFileName --> $OutFileName)\n" : "... ";
301 print STDERR "Translating$zzz";
302 open (OUTFILE,">$OutFileName");
304 # Open the file to turn into LyX.
305 my $infile = new Text::TeX::OpenFile $InFileName,
306 'defaultact' => \&basic_lyx,
307 'tokens' => \%MyTokens;
309 # Process what's left of the file (everything from \begin{document})
312 # Last line of the LyX file
313 print OUTFILE "\n\\the_end\n";
315 #warn "Done with basic translation\n";
317 } # end subroutine call_parser
319 # This is used as a toggle so that we know what to do when basic_lyx is
320 # passed a '$' or '$$' token.
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) {
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) {
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%');
365 my $LatexLength = shift;
368 # If $LatexLength is something like '4.5\columnwidth', translate into
370 if ($LatexLength =~ /([0-9]+\.?[0-9]*)\s*(\\[a-z]*)/) {
371 if (defined($lengthAsLyXString{$2})) {
372 $LyXLength = ($1 * 100) . $lengthAsLyXString{$2};
375 $LyXLength = $LatexLength;
376 # Remove any spaces from '4.5 cm'
377 $LyXLength =~ s/\s*//g;
383 ########################## MAIN TRANSLATOR SUBROUTINE #####################
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)
390 my $fileobject = shift;
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;
396 my $type = ref($eaten);
397 print "$type " if $debug_on;
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) {
405 s/^Text::TeX:://o or die "unknown token?!";
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) {
425 # Handle blank lines.
426 if (m/^Paragraph$/o) {
427 # $INP <>0 means We will need a new layout command
430 # $MBD means start a begin_deeper within list environments
431 # unless there's an \item command
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;
441 # Handle LaTeX tokens
444 my $name = $eaten->token_name; # name of the token, e.g., "\large"
445 print "'$name' " if $debug_on;
447 # Tokens which turn into a bit of LyX text
448 if (exists $TextTokenTransTable{$name}) {
449 &CheckForNewParagraph; #Start new paragraph if necessary
451 my $to_print = $TextTokenTransTable{$name};
453 # \@ has to be specially handled, because it depends on
454 # the character AFTER the \@
456 my $next = $fileobject->eatGroup(1);
458 $ch = $next->print or warn "\@ confused me!\n";
460 # Note: \@ CAN'T have pre_space before it
461 print OUTFILE "$to_print$ch\n";
462 print "followed by $ch" if $debug_on;
464 warn "LyX (or LaTeX) can't handle '$ch' after $name\n";
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 \@
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 ",
479 "\n\n\\end_inset \n\n";
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.
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;
493 } elsif (ending_math($name)) {
495 print OUTFILE "$name\n\\end_inset \n\n";
496 print "\nDone copying math ending with '$name'" if $debug_on;
498 # Items in list environments
499 } elsif ($name eq '\item') {
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"
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
516 # But since we had an \item command, DON'T nest a
517 # deeper "Standard" paragraph in the list
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 [
533 # Special lists (e.g. List layout) have to print something
534 # before each item. In that case, CFNP and print it
536 &CheckForNewParagraph;
537 print OUTFILE $item_preface;
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'
546 if (! $IsNewParagraph) {
547 print OUTFILE "$pre_space$command";
548 } #otherwise, wait until we've printed the new paragraph command
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}}"
558 } elsif ($name eq '&') {
559 if ($thistable = &RelyxTable::in_table) {
560 print OUTFILE "\n\\newline \n";
562 } else {warn "& is illegal outside a table!"}
564 } elsif ($name eq '\\\\' || $name eq '\\newline' || $name eq "\\tabularnewline") {
565 &CheckForNewParagraph; # could be at beginning of par?
566 print OUTFILE "\n\\newline \n";
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) {
576 } elsif ($name eq '\hline') {
577 if ($thistable = &RelyxTable::in_table) {
578 # hcline does hline if no arg is given
580 } else {warn "\\hline is illegal outside a table!"}
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!";
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;
599 $length =~ s/\s*$//; # may have \n at end
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");
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);
612 # Otherwise it's an unknown token, which must be copied
613 # in TeX mode, along with its arguments, if any
615 if (defined($eaten->relyx_args($fileobject))) {
616 ©_latex_known($eaten, $fileobject);
617 } else { # it's not in MyTokens
618 ©_latex_unknown($eaten, $fileobject);
625 # Handle tokens that take arguments, like \section{},\section*{}
626 if (/^BegArgsToken$/) {
627 my $name = $eaten->token_name;
628 print "$name" if $debug_on;
630 # Handle things that LyX translates as a "LatexCommand" inset
631 if (foundIn($name, @LatexCommands) || isNatbibCitation($name)){
632 &CheckForNewParagraph; #Start new paragraph if necessary
634 print OUTFILE "$pre_space\n\\begin_inset LatexCommand ";
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, "]";
648 print OUTFILE "$name";
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;
660 last TYPESW; # skip to the end of the switch
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
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.)
675 print OUTFILE $eaten->exact_print;
679 # Sectioning and Title environments (using a LyX \layout command)
680 if (exists $ReadCommands::ToLayout->{$name}) {
681 &ConvertToLayout($name);
682 last TYPESW; #done translating
684 # Font characteristics
685 } elsif (exists $FontTransTable{$name}) {
687 my $command = $FontTransTable{$name};
688 ($dummy, $dum2) = ($command =~ /(\S+)\s+(\w+)/);
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/;
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";
703 # Store the current font change
704 die "Font command error" if !exists $$CurrentFontStatus{$dummy};
705 push (@{$CurrentFontStatus->{$dummy}}, $dum2);
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};
715 print OUTFILE "$pre_space$command";
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"],
728 # And make $CurrentLayoutStack point to a new (anon.) stack
729 $CurrentLayoutStack = ["Standard"];
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;
738 $MayBeDeeper = 0; #can't be deeper at beginning of footnote
739 $CurrentAlignment = "";
742 } elsif ($name =~ m/^$AccentTokens$/) {
743 &CheckForNewParagraph; # may be at the beginning of a par
745 print OUTFILE "$pre_space\n",'\i ',$name,'{'
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 .
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;
766 &print_tex_mode($eaten->exact_print . $save);
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!"}
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!"}
789 } elsif ($name eq '\bibliographystyle') {
790 $tok = $fileobject->eatRequiredArgument;
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;
799 print "\nBibliography style file is $bibstyle_file"if $debug_on;
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;
808 $tok = $fileobject->eatOptionalArgument;
809 print OUTFILE "$name ", $tok->exact_print, "{";
813 # ensuremath -- copy verbatim until you're done
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.
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;
829 print OUTFILE "\\)\n\\end_inset \n\n";
830 print "\nDone copying math" if $debug_on;
832 # Token in the ReadCommands command list that basic_lyx doesn't
835 ©_latex_known($eaten,$fileobject);
842 # ArgTokens appear when we've used eatRequiredArgument
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 ©_latex_known($eaten,$fileobject);
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 ©_latex_known($eaten,$fileobject);
861 my $name = $eaten->token_name;
862 print "$name" if $debug_on;
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";
870 } elsif (exists $ReadCommands::ToLayout->{$name}) {
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";
883 # Footnotes and marginpars
884 } elsif (exists $FloatTransTable{$name}) {
885 print OUTFILE "\n\\end_float \n\n";
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;
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};
900 # Same paragraph status as we had before the footnote started
901 $IsNewParagraph = $OldINP; $MayBeDeeper = $OldMBD;
902 $CurrentAlignment = $OldAlignment;
904 } elsif ($name =~ m/^$AccentTokens$/) {
907 } elsif ($name eq "\\bibitem") {
913 } # end if EndArgsToken
915 # Handle END of scope of local commands like \large
917 my $name = $eaten->token_name; #cmd we're ending, e.g.,\large
918 print $name if $debug_on;
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}}"
930 my $newfont = $CurrentFontStatus->{$dummy}->[-1];
931 $command = "\n$dummy $newfont\n";
932 print OUTFILE "$command";
935 warn "Unknown EndLocal token!\n";
941 # We don't print { or }, but we make sure that the spacing is correct
943 if (/^Begin::Group$/) {
944 print OUTFILE "$pre_space";
949 if (/^End::Group$/) {
950 print OUTFILE "$pre_space";
955 if (/^Begin::Group::Args$/) {
956 print $eaten->print," " if $debug_on; # the string "\begin{foo}"
957 my $env = $eaten->environment;
959 # Any environment found in the layouts files
960 if (exists $ReadCommands::ToLayout->{$env}) {
961 &ConvertToLayout($env);
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;
971 if ($ExtraArgEnvironments{$env}) { #print it
972 print "\nArgument $arg to $env environment"
974 $item_preface = $ExtraArgEnvironments{$env} . $arg."\n";
977 print "\nIgnoring argument '$arg' to $env environment"
980 } # end if for reading extra args to \begin command
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"
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;
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;
1001 # alignment environments automatically start a new paragraph
1002 $IsNewParagraph = 1;
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;
1010 $tok = $fileobject->eatOptionalArgument;
1011 if ($tok && defined ($dummy = $tok->print) && $dummy) {
1012 print "\nIgnoring float placement '$dummy'" if $debug_on;
1014 my $command = $FloatEnvTransTable{$env};
1016 # Open the table/figure
1017 print OUTFILE "$command";
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;
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";
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;
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;
1043 } elsif ($env eq "minipage") {
1044 &CheckForNewParagraph;
1046 print OUTFILE "\\begin_inset Minipage\n";
1048 # The minipage environment is defined as:
1049 # \begin{minipage}[pos][height][inner-pos]{width} <text>
1052 # Read the position optional argument, if it exists
1053 $tok = $fileobject->eatOptionalArgument;
1054 my $pos = $tok->print if defined($tok->print);
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";
1061 # The minipage is centred by default.
1062 $pos = '1' if (!defined($map{$pos}) ||
1063 ($pos = $map{$pos}) eq '');
1065 # Read the height optional argument, if it exists
1067 $tok = $fileobject->eatOptionalArgument;
1068 if (defined($tok->print)) {
1069 $height = getAsLyXLength($tok->print);
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");
1081 # Read the width as (a reference to) an array of tokens.
1082 $tok = $fileobject->eatBalanced;
1083 my $width = getAsLyXLength($tok->exact_print);
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";
1092 } elsif ($env eq "document") {
1094 #print "\nStarting to translate actual document" if $debug_on;
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"
1102 $dummy = $eaten->print; # \begin{env}, ignore initial whitespace
1103 &print_tex_mode($dummy);
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}
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);
1124 if (/^End::Group::Args$/) {
1125 print $eaten->print," " if $debug_on; # the string "\end{foo}"
1126 my $env = $eaten->environment;
1128 # End of list or quote/verse environment
1129 # OR special environment given with -t option
1130 if (exists $ReadCommands::ToLayout->{$env}) {
1132 $item_preface = ""; # reset when at end of List env.
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;
1139 } elsif (exists $AlignEnvironments{$env}) {
1140 # Back to block alignment
1141 $CurrentAlignment = "";
1142 print "\nBack to block alignment" if $debug_on;
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;
1148 # Environments lyx translates to floats
1149 } elsif (exists $FloatEnvTransTable{$env}) {
1150 print OUTFILE "\n\\end_float \n\n";
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!"}
1159 # Anything after a table will be a new paragraph
1160 $IsNewParagraph = 1; $MayBeDeeper = 1;
1163 } elsif ($env eq "minipage") {
1164 print OUTFILE "\n\\end_inset \n\n";
1166 # Next stuff will be new env.
1167 # $IsNewParagraph = 1;
1169 } elsif ($env eq "document") {
1170 print "\nDone with document!" if $debug_on;
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);
1177 # Next stuff will be new env.
1178 $IsNewParagraph = 1;
1180 # End of unknown environments. We're already in TeX mode
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";
1187 &print_tex_mode($tex_mode_string);
1188 print "Done copying unknown environment '$env'" if $debug_on;
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.
1206 my $outstr = $eaten->print; # the actual text
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
1212 last TYPESW if $outstr =~ /^\s+$/;
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/;
1220 # Only write '\layout Standard' once per paragraph
1221 &CheckForNewParagraph;
1223 # \n\n signals end of paragraph, so get rid of it (and any space
1225 $outstr =~ s/\s*\n\n$//;
1227 # Print the LaTeX text to STDOUT
1228 print "'$outstr'" if $debug_on;
1230 # LyX *ignores* \n and \t, whereas LaTeX considers them just
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;
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{'~'};
1241 # This will not handle brackets in braces!
1242 if ($outstr =~ /\]/) { # protect spaces only *until* the bracket
1245 # Note that any \t's have been changed to space already
1246 $tempstr =~ s/ /$dummy/g;
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;
1257 } # end special stuff for protected spaces
1259 # Translate any LaTeX text that requires special LyX handling
1260 foreach $dummy (keys %TextTransTable) {
1261 $outstr =~ s/\Q$dummy\E/$TextTransTable{$dummy}/g;
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
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
1278 # Actually print the text
1279 print OUTFILE "$outstr";
1281 } # end TT::Text handling
1283 # The default action - this should never happen!
1284 print("I don't know ",$eaten->print) if $debug_on;
1288 print "\n" if $debug_on;
1290 } #end sub basic_lyx
1292 ######################### TEX MODE SUBROUTINES #########################
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 {
1299 my $fileobject = shift;
1300 my $outstr = $eaten->exact_print;
1301 my ($dummy, $tok, $count);
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"
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
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;
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
1328 } # end if $dummy = [{
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 '}';
1335 # Print it out in TeX mode
1336 &print_tex_mode($outstr);
1338 print "\nDone copying unknown token" if $debug_on;
1339 } # end sub copy_latex_unknown
1341 # Copy an untranslatable latex command whose syntax we know, along with its
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?!";
1356 # token itself for TT::Token, TT::BegArgsToken,
1357 # Corresponding BegArgsToken for ArgToken,EndArgsToken
1358 my $temp_start = $eaten->base_token;
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;
1366 # initialize the string of stuff we're copying
1367 $tex_mode_string = $eaten->exact_print;
1368 } # Start tex copying?
1371 # This token's next arguments -- returns a string matching /o*[rR]?/
1372 my $curr_args = $eaten->next_args($fileobject);
1374 if ($type eq "EndArgsToken" or $type eq "ArgToken") {
1375 # Print ending '}' for the group we just finished reading
1376 $tex_mode_string .= '}';
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;
1384 $curr_args =~ s/^o*//; # Some OptArgs may not have appeared
1386 if ($type eq "BegArgsToken" or $type eq "ArgToken") {
1387 # Print beginning '{' for the group we're about to read
1388 $tex_mode_string .= '{';
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;
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;
1407 } else { # anything but '' is weird
1408 warn "weird arg $curr_args to ",$temp_start->print,"\n" if $curr_args;
1411 # Finished tex mode copying
1412 if ($type eq "Token" or $type eq "EndArgsToken") {
1414 # Add {} to plain tokens followed by { or }. Otherwise {\foo}bar
1415 # and \foo{bar} yield \foobar in the LaTeX files created by LyX
1417 if ($type eq "Token" and
1418 $dummy=$fileobject->lookAheadToken and
1421 $tex_mode_string .= '{}';
1424 # Print out the string
1425 print "\n" if $debug_on;
1426 &print_tex_mode($tex_mode_string);
1427 $tex_mode_string = "";
1429 # We're done with this token
1430 pop(@tex_mode_tokens);
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"
1436 } # end tex copying?
1437 } # end sub copy_latex_known
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 {
1450 print "'$outstr'" if $debug_on;
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}'
1457 # Any whitespace is equivalent to one space in LaTeX
1458 $outstr =~ s/^\s+/ /; # e.g. $outstr follows "\LaTeX{}" or "Hi{}"
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;
1466 print OUTFILE "$start_tex_mode";
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;
1473 # Print the actual token + arguments if any
1474 print OUTFILE $outstr;
1476 # Get OUT of LaTeX mode (and end nesting if nec.)
1477 print OUTFILE "$end_tex_mode";
1478 $IsNewParagraph = $str_ends_par;
1481 } # end sub print_tex_mode
1483 ############################ LAYOUT SUBROUTINES ###########################
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
1496 my $layout = $$CurrentLayoutStack[-1];
1498 return if &RelyxTable::in_table;
1500 if ($IsNewParagraph) {
1501 # Handle standard text within a list environment specially
1503 if ($layout =~ /^$ListLayouts$/o) {
1504 push (@$CurrentLayoutStack, "Standard");
1505 print "\nCurrent Layout Stack: @$CurrentLayoutStack\n"
1507 print OUTFILE "\n\\begin_deeper ";
1508 $layout = "Standard";
1510 $MayBeDeeper = 0; # Don't test again until new paragraph
1513 # Print layout command itself
1514 print OUTFILE "\n\\layout $layout\n\n";
1515 print OUTFILE $CurrentAlignment if $CurrentAlignment;
1517 # Now that we've written the command, it's no longer a new paragraph
1518 $IsNewParagraph = 0;
1520 # And we're no longer waiting to see if this paragraph is empty
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";
1531 } # end sub CheckForNewParagraph
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
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;
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 ";
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;
1562 # Put the new layout onto the layout stack
1563 push @$CurrentLayoutStack, $layout;
1564 print "\nCurrent Layout Stack: @$CurrentLayoutStack" if $debug_on;
1566 # Upcoming text will be new paragraph, needing a new layout cmd
1567 $IsNewParagraph = 1;
1569 # Test for nested "Standard" paragraph in upcoming text?
1570 # Some environments can nest. Sections & Title commands can't
1571 $MayBeDeeper = $layoutref->{"nestable"};
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
1576 $PendingLayout = $keepempty;
1578 } # end sub ConvertToLayout
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
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;
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;
1600 my $mylayout = pop (@$CurrentLayoutStack);
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);
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;
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
1625 if ($#{$CurrentLayoutStack} > 0) {
1626 print " End Nesting!" if $debug_on;
1627 print OUTFILE "\n\\end_deeper ";
1630 # Upcoming text will be new paragraph, needing a new layout cmd
1631 $IsNewParagraph = 1;
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
1638 ####################### MISCELLANEOUS SUBROUTINES ###########################
1640 # Translate math commands LyX doesn't support into commands it does support
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
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
1658 defined $4 && $tok =~ /[A-Za-z]$/ && $input =~ /^[A-Za-z]/;
1662 $output .= $input; # copy what's left in $input
1667 1; # return true to calling subroutine