From 51790e63214bfd245bdf09d62e0c39a1ffac1a60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uwe=20St=C3=B6hr?= Date: Thu, 27 Oct 2011 02:29:03 +0000 Subject: [PATCH] tex2lyx: support for subfloats git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@40025 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/tex2lyx/TODO.txt | 1 - src/tex2lyx/preamble.cpp | 10 ++++++ src/tex2lyx/text.cpp | 75 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 84 insertions(+), 2 deletions(-) diff --git a/src/tex2lyx/TODO.txt b/src/tex2lyx/TODO.txt index 12e1d37796..a6191cb967 100644 --- a/src/tex2lyx/TODO.txt +++ b/src/tex2lyx/TODO.txt @@ -30,7 +30,6 @@ Format LaTeX feature LyX feature 293 ? InsetInfo 309 \nocite InsetCitation 310 \nocite{*} InsetBibtex -316 subfig.sty (subfloats) InsetFloat 322 ? local layout 325 japanese one japanese language 326 PDFLaTeX for external insets InsetExternal diff --git a/src/tex2lyx/preamble.cpp b/src/tex2lyx/preamble.cpp index 7ba86a2430..e31a2a8252 100644 --- a/src/tex2lyx/preamble.cpp +++ b/src/tex2lyx/preamble.cpp @@ -654,6 +654,9 @@ void handle_package(Parser &p, string const & name, string const & opts, else if (name == "wrapfig") ; // ignore this + else if (name == "subfig") + ; // ignore this + else if (is_known(name, known_languages)) h_language = name; @@ -1280,6 +1283,13 @@ void parse_preamble(Parser & p, ostream & os, } else if (arg1 == "definecolor" && arg2 == "\\usepackage{color}" && arg3.empty()) { ifundefined_color_set = true; + // test for case + //\@ifundefined{showcaptionsetup}{}{% + // \PassOptionsToPackage{caption=false}{subfig}} + // that LyX uses for subfloats + } else if (arg1 == "showcaptionsetup" && arg2.empty() + && arg3 == "%\n \\PassOptionsToPackage{caption=false}{subfig}") { + ; // do nothing } else if (!in_lyx_preamble) { h_preamble << t.asInput() << '{' << arg1 << '}' diff --git a/src/tex2lyx/text.cpp b/src/tex2lyx/text.cpp index a2bd665c8f..df639dbc00 100644 --- a/src/tex2lyx/text.cpp +++ b/src/tex2lyx/text.cpp @@ -229,6 +229,9 @@ char const * const known_phrases[] = {"LyX", "TeX", "LaTeXe", "LaTeX", 0}; char const * const known_coded_phrases[] = {"LyX", "TeX", "LaTeX2e", "LaTeX", 0}; int const known_phrase_lengths[] = {3, 5, 7, 0}; +// string to store the float type to be able to determine the type of subfloats +string float_type = ""; + /// splits "x=z, y=b" into a map and an ordered keyword vector void split_map(string const & s, map & res, vector & keys) @@ -1062,6 +1065,14 @@ void parse_environment(Parser & p, ostream & os, bool outer, eat_whitespace(p, os, parent_context, false); parent_context.check_layout(os); begin_inset(os, "Float " + unstarred_name + "\n"); + // store the float type for subfloats + // subfloats only work with figures and tables + if (unstarred_name == "figure") + float_type = unstarred_name; + else if (unstarred_name == "table") + float_type = unstarred_name; + else + float_type = ""; if (p.hasOpt()) os << "placement " << p.getArg('[', ']') << '\n'; os << "wide " << convert(is_starred) @@ -2133,7 +2144,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer, p.skip_spaces(); context.check_layout(os); p.skip_spaces(); - begin_inset(os, "Caption\n\n"); + begin_inset(os, "Caption\n"); Context newcontext(true, context.textclass); newcontext.font = context.font; newcontext.check_layout(os); @@ -2156,6 +2167,68 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer, newcontext.check_end_layout(os); } + else if (t.cs() == "subfloat") { + // the syntax is \subfloat[caption]{content} + // if it is a table of figure depends on the surrounding float + bool has_caption = false; + p.skip_spaces(); + // do nothing if there is no outer float + if (!float_type.empty()) { + context.check_layout(os); + p.skip_spaces(); + begin_inset(os, "Float " + float_type + "\n"); + os << "wide false" + << "\nsideways false" + << "\nstatus collapsed\n\n"; + // test for caption + string caption; + if (p.next_token().cat() != catEscape && + p.next_token().character() == '[') { + p.get_token(); // eat '[' + caption = parse_text_snippet(p, FLAG_BRACK_LAST, outer, context); + has_caption = true; + } + // the content + parse_text_in_inset(p, os, FLAG_ITEM, outer, context); + // the caption comes always as the last + if (has_caption) { + // we must make sure that the caption gets a \begin_layout + os << "\n\\begin_layout Plain Layout"; + p.skip_spaces(); + begin_inset(os, "Caption\n"); + Context newcontext(true, context.textclass); + newcontext.font = context.font; + newcontext.check_layout(os); + os << caption << "\n"; + newcontext.check_end_layout(os); + // We don't need really a new paragraph, but + // we must make sure that the next item gets a \begin_layout. + //newcontext.new_paragraph(os); + end_inset(os); + p.skip_spaces(); + } + // We don't need really a new paragraph, but + // we must make sure that the next item gets a \begin_layout. + if (has_caption) + context.new_paragraph(os); + end_inset(os); + p.skip_spaces(); + context.check_end_layout(os); + // close the layout we opened + if (has_caption) + os << "\n\\end_layout\n"; + } else { + // if the float type is not supported or there is no surrounding float + // output it as ERT + if (p.hasOpt()) { + string opt_arg = convert_command_inset_arg(p.getArg('[', ']')); + handle_ert(os, t.asInput() + '[' + opt_arg + + "]{" + p.verbatim_item() + '}', context); + } else + handle_ert(os, t.asInput() + "{" + p.verbatim_item() + '}', context); + } + } + else if (t.cs() == "includegraphics") { bool const clip = p.next_token().asInput() == "*"; if (clip)