3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Jean-Marc Lasgouttes
10 * Full author contact details are available in file CREDITS.
21 #include "support/lassert.h"
22 #include "support/convert.h"
23 #include "support/lstrings.h"
40 ColInfo() : align('n'), valign('n'), rightlines(0), leftlines(0) {}
43 /// vertical alignment
47 /// special column alignment
49 /// number of lines on the right
51 /// number of lines on the left
56 /// row type for longtables
63 /// part of head on first page
67 /// part of foot on last page
74 RowInfo() : topline(false), bottomline(false), type(LT_NORMAL),
75 caption(false), newpage(false) {}
76 /// Does this row have any special setting?
79 return topline || bottomline || !top_space.empty() ||
80 !bottom_space.empty() || !interline_space.empty() ||
81 type != LT_NORMAL || caption || newpage;
83 /// horizontal line above
85 /// horizontal line below
87 /// Extra space between the top line and this row
89 /// Extra space between this row and the bottom line
91 /// Extra space between the bottom line and the next top line
92 string interline_space;
93 /// These are for longtabulars only
94 /// row type (head, foot, firsthead etc.)
103 /// the numeric values are part of the file format!
107 /// A multicolumn cell. The number of columns is <tt>1 + number
108 /// of CELL_PART_OF_MULTICOLUMN cells</tt> that follow directly
109 CELL_BEGIN_OF_MULTICOLUMN,
110 /// This is a dummy cell (part of a multicolumn cell)
111 CELL_PART_OF_MULTICOLUMN,
113 CELL_BEGIN_OF_MULTIROW,
115 CELL_PART_OF_MULTIROW
121 CellInfo() : multi(CELL_NORMAL), align('n'), valign('n'),
122 leftlines(0), rightlines(0), topline(false),
123 bottomline(false), rotate(0), mrxnum(0) {}
130 /// vertical cell alignment
132 /// number of lines on the left
134 /// number of lines on the right
136 /// do we have a line above?
138 /// do we have a line below?
140 /// how is the cell rotated?
142 /// width for multicolumn cells
144 /// special formatting for multicolumn cells
148 /// number of further multirows
156 ltType() : set(false), topDL(false), bottomDL(false), empty(false) {}
157 // we have this header type (is set in the getLT... functions)
159 // double borders on top
161 // double borders on bottom
163 // used for FirstHeader & LastFooter and if this is true
164 // all the rows marked as FirstHeader or LastFooter are
165 // ignored in the output and it is set to be empty!
170 /// translate a horizontal alignment (as stored in ColInfo and CellInfo) to LyX
171 inline char const * verbose_align(char c)
186 /// translate a vertical alignment (as stored in ColInfo and CellInfo) to LyX
187 inline char const * verbose_valign(char c)
189 // The default value for no special alignment is "top".
202 // stripped down from tabluar.C. We use it currently only for bools and
204 string const write_attribute(string const & name, bool const & b)
206 // we write only true attribute values so we remove a bit of the
207 // file format bloat for tabulars.
208 return b ? ' ' + name + "=\"true\"" : string();
212 string const write_attribute(string const & name, string const & s)
214 return s.empty() ? string() : ' ' + name + "=\"" + s + '"';
218 string const write_attribute(string const & name, int const & i)
220 // we write only true attribute values so we remove a bit of the
221 // file format bloat for tabulars.
222 return i ? write_attribute(name, convert<string>(i)) : string();
226 /*! rather brutish way to code table structure in a string:
231 \multicolumn{2}{c}{4} & 5 //
237 gets "translated" to:
240 HLINE 1 TAB 2 TAB 3 HLINE HLINE LINE
241 \hline HLINE \multicolumn{2}{c}{4} TAB 5 HLINE HLINE LINE
242 HLINE 6 TAB 7 HLINE HLINE LINE
243 HLINE 8 HLINE \endhead HLINE LINE
247 char const TAB = '\001';
248 char const LINE = '\002';
249 char const HLINE = '\004';
253 * Move the information in leftlines, rightlines, align and valign to the
254 * special field. This is necessary if the special field is not empty,
255 * because LyX ignores leftlines > 1, rightlines > 1, align and valign in
258 void ci2special(ColInfo & ci)
260 if (ci.width.empty() && ci.align == 'n')
261 // The alignment setting is already in special, since
262 // handle_colalign() never stores ci with these settings
263 // and ensures that leftlines == 0 and rightlines == 0 in
267 if (!ci.width.empty()) {
270 ci.special += ">{\\raggedright}";
273 ci.special += ">{\\raggedleft}";
276 ci.special += ">{\\centering}";
279 if (ci.valign == 'n')
282 ci.special += ci.valign;
283 ci.special += '{' + ci.width + '}';
286 ci.special += ci.align;
288 // LyX can only have one left and one right line.
289 for (int i = 1; i < ci.leftlines; ++i)
290 ci.special.insert(0, "|");
291 for (int i = 1; i < ci.rightlines; ++i)
293 ci.leftlines = min(ci.leftlines, 1);
294 ci.rightlines = min(ci.rightlines, 1);
301 * Handle column specifications for tabulars and multicolumns.
302 * The next token of the parser \p p must be an opening brace, and we read
303 * everything until the matching closing brace.
304 * The resulting column specifications are filled into \p colinfo. This is
305 * in an intermediate form. fix_colalign() makes it suitable for LyX output.
307 void handle_colalign(Parser & p, vector<ColInfo> & colinfo,
308 ColInfo const & start)
310 if (p.get_token().cat() != catBegin)
311 cerr << "Wrong syntax for table column alignment.\n"
312 "Expected '{', got '" << p.curr_token().asInput()
315 ColInfo next = start;
316 for (Token t = p.get_token(); p.good() && t.cat() != catEnd;
319 cerr << "t: " << t << " c: '" << t.character() << "'\n";
322 // We cannot handle comments here
323 if (t.cat() == catComment) {
324 if (t.cs().empty()) {
328 cerr << "Ignoring comment: " << t.asInput();
332 switch (t.character()) {
334 // whitespace, ignore.
339 // new column, horizontal aligned
340 next.align = t.character();
341 if (!next.special.empty())
343 colinfo.push_back(next);
349 // new column, vertical aligned box
350 next.valign = t.character();
351 next.width = p.verbatim_item();
352 if (!next.special.empty())
354 colinfo.push_back(next);
359 if (colinfo.empty()) {
360 if (next.special.empty())
364 } else if (colinfo.back().special.empty())
365 ++colinfo.back().rightlines;
366 else if (next.special.empty())
369 colinfo.back().special += '|';
372 // text before the next column
373 string const s = trimSpaceAndEol(p.verbatim_item());
374 if (next.special.empty() &&
376 // Maybe this can be converted to a
377 // horizontal alignment setting for
378 // fixed width columns
379 if (s == "\\raggedleft")
381 else if (s == "\\raggedright")
383 else if (s == "\\centering")
386 next.special = ">{" + s + '}';
388 next.special += ">{" + s + '}';
392 // text after the last column
393 string const s = trimSpaceAndEol(p.verbatim_item());
395 // This is not possible in LaTeX.
396 cerr << "Ignoring separator '<{"
397 << s << "}'." << endl;
399 ColInfo & ci = colinfo.back();
401 ci.special += "<{" + s + '}';
406 if (p.next_token().character() != '{')
408 // *{n}{arg} means 'n' columns of type 'arg'
409 string const num = p.verbatim_item();
410 string const arg = p.verbatim_item();
411 size_t const n = convert<unsigned int>(num);
412 if (!arg.empty() && n > 0) {
414 for (size_t i = 0; i < n; ++i)
418 handle_colalign(p2, colinfo, next);
421 cerr << "Ignoring column specification"
422 " '*{" << num << "}{"
423 << arg << "}'." << endl;
428 // text instead of the column spacing
430 // text in addition to the column spacing
431 next.special += t.character();
432 next.special += '{' + p.verbatim_item() + '}';
435 // try user defined column types
436 // unknown column types (nargs == -1) are
437 // assumed to consume no arguments
439 next.special += t.character();
441 preamble.getSpecialTableColumnArguments(t.character());
442 for (int i = 0; i < nargs; ++i)
443 next.special += '{' +
444 p.verbatim_item() + '}';
445 colinfo.push_back(next);
452 // Maybe we have some column separators that need to be added to the
455 if (!next.special.empty()) {
456 ColInfo & ci = colinfo.back();
458 ci.special += next.special;
459 next.special.erase();
465 * Move the left and right lines and alignment settings of the column \p ci
466 * to the special field if necessary.
468 void fix_colalign(ColInfo & ci)
470 if (ci.leftlines > 1 || ci.rightlines > 1)
476 * LyX can't handle more than one vertical line at the left or right side
478 * This function moves the left and right lines and alignment settings of all
479 * columns in \p colinfo to the special field if necessary.
481 void fix_colalign(vector<ColInfo> & colinfo)
483 // Try to move extra leftlines to the previous column.
484 // We do this only if both special fields are empty, otherwise we
485 // can't tell wether the result will be the same.
486 for (size_t col = 0; col < colinfo.size(); ++col) {
487 if (colinfo[col].leftlines > 1 &&
488 colinfo[col].special.empty() && col > 0 &&
489 colinfo[col - 1].rightlines == 0 &&
490 colinfo[col - 1].special.empty()) {
491 ++colinfo[col - 1].rightlines;
492 --colinfo[col].leftlines;
495 // Try to move extra rightlines to the next column
496 for (size_t col = 0; col < colinfo.size(); ++col) {
497 if (colinfo[col].rightlines > 1 &&
498 colinfo[col].special.empty() &&
499 col < colinfo.size() - 1 &&
500 colinfo[col + 1].leftlines == 0 &&
501 colinfo[col + 1].special.empty()) {
502 ++colinfo[col + 1].leftlines;
503 --colinfo[col].rightlines;
506 // Move the lines and alignment settings to the special field if
508 for (size_t col = 0; col < colinfo.size(); ++col)
509 fix_colalign(colinfo[col]);
514 * Parse hlines and similar stuff.
515 * \returns wether the token \p t was parsed
517 bool parse_hlines(Parser & p, Token const & t, string & hlines,
518 bool is_long_tabular)
520 LASSERT(t.cat() == catEscape, return false);
522 if (t.cs() == "hline" || t.cs() == "toprule" || t.cs() == "midrule" ||
523 t.cs() == "bottomrule")
524 hlines += '\\' + t.cs();
526 else if (t.cs() == "cline")
527 hlines += "\\cline{" + p.verbatim_item() + '}';
529 else if (t.cs() == "cmidrule") {
530 // We cannot handle the \cmidrule(l){3-4} form
533 bool const hasParentheses(p.getFullArg('(', ')').first);
537 hlines += "\\cmidrule{" + p.verbatim_item() + '}';
540 else if (t.cs() == "addlinespace") {
543 bool const hasArgument(p.getFullArg('{', '}').first);
546 hlines += "\\addlinespace{" + p.verbatim_item() + '}';
548 hlines += "\\addlinespace";
551 else if (is_long_tabular && t.cs() == "newpage")
552 hlines += "\\newpage";
561 /// Position in a row
563 /// At the very beginning, before the first token
565 /// After the first token and before any column token
567 /// After the first column token. Comments and whitespace are only
568 /// treated as tokens in this position
570 /// After the first non-column token at the end
576 * Parse table structure.
577 * We parse tables in a two-pass process: This function extracts the table
578 * structure (rows, columns, hlines etc.), but does not change the cell
579 * content. The cell content is parsed in a second step in handle_tabular().
581 void parse_table(Parser & p, ostream & os, bool is_long_tabular,
582 RowPosition & pos, unsigned flags)
584 // table structure commands such as \hline
587 // comments that occur at places where we can't handle them
591 Token const & t = p.get_token();
594 debugToken(cerr, t, flags);
597 // comments and whitespace in hlines
600 case IN_HLINES_START:
602 if (t.cat() == catComment) {
607 // We can't handle comments here,
608 // store them for later use
609 comments += t.asInput();
611 } else if (t.cat() == catSpace ||
612 t.cat() == catNewline) {
613 // whitespace is irrelevant here, we
614 // need to recognize hline stuff
623 // We need to handle structure stuff first in order to
624 // determine wether we need to output a HLINE separator
625 // before the row or not.
626 if (t.cat() == catEscape) {
627 if (parse_hlines(p, t, hlines, is_long_tabular)) {
630 pos = IN_HLINES_START;
635 case IN_HLINES_START:
642 else if (t.cs() == "tabularnewline" ||
646 cerr << "Warning: Converting TeX "
647 "'\\cr' to LaTeX '\\\\'."
649 // stuff before the line break
650 os << comments << HLINE << hlines << HLINE
652 //cerr << "hlines: " << hlines << endl;
659 else if (is_long_tabular &&
660 (t.cs() == "endhead" ||
661 t.cs() == "endfirsthead" ||
662 t.cs() == "endfoot" ||
663 t.cs() == "endlastfoot")) {
664 hlines += t.asInput();
668 // these commands are implicit line
670 os << comments << HLINE << hlines
677 pos = IN_HLINES_START;
679 case IN_HLINES_START:
686 // We need a HLINE separator if we either have no hline
687 // stuff at all and are just starting a row or if we just
688 // got the first non-hline token.
691 // no hline tokens exist, first token at row start
692 case IN_HLINES_START:
693 // hline tokens exist, first non-hline token at row
695 os << hlines << HLINE << comments;
701 // Oops, there is still cell content or unsupported
702 // booktabs commands after hline stuff. The latter are
703 // moved to the cell, and the first does not work in
704 // LaTeX, so we ignore the hlines.
707 if (support::contains(hlines, "\\hline") ||
708 support::contains(hlines, "\\cline") ||
709 support::contains(hlines, "\\newpage"))
710 cerr << "Ignoring '" << hlines
711 << "' in a cell" << endl;
721 // If we come here we have normal cell content
725 if (t.cat() == catMath) {
726 // we are inside some text mode thingy, so opening new math is allowed
727 Token const & n = p.get_token();
728 if (n.cat() == catMath) {
729 // TeX's $$...$$ syntax for displayed math
731 // This does only work because parse_math outputs TeX
732 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
734 p.get_token(); // skip the second '$' token
736 // simple $...$ stuff
739 // This does only work because parse_math outputs TeX
740 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
745 else if (t.cat() == catSpace
746 || t.cat() == catNewline
747 || t.cat() == catLetter
748 || t.cat() == catSuper
750 || t.cat() == catOther
751 || t.cat() == catActive
752 || t.cat() == catParameter)
755 else if (t.cat() == catBegin) {
757 parse_table(p, os, is_long_tabular, pos,
762 else if (t.cat() == catEnd) {
763 if (flags & FLAG_BRACE_LAST)
765 cerr << "unexpected '}'\n";
768 else if (t.cat() == catAlign) {
773 else if (t.cat() == catComment)
776 else if (t.cs() == "(") {
778 // This does only work because parse_math outputs TeX
779 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
783 else if (t.cs() == "[") {
785 // This does only work because parse_math outputs TeX
786 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
790 else if (t.cs() == "begin") {
791 string const name = p.getArg('{', '}');
792 active_environments.push_back(name);
793 os << "\\begin{" << name << '}';
794 // treat the nested environment as a block, don't
795 // parse &, \\ etc, because they don't belong to our
796 // table if they appear.
797 os << p.ertEnvironment(name);
798 os << "\\end{" << name << '}';
799 active_environments.pop_back();
802 else if (t.cs() == "end") {
803 if (flags & FLAG_END) {
804 // eat environment name
805 string const name = p.getArg('{', '}');
806 if (name != active_environment())
807 p.error("\\end{" + name + "} does not match \\begin{"
808 + active_environment() + "}");
811 p.error("found 'end' unexpectedly");
818 // We can have comments if the last line is incomplete
821 // We can have hline stuff if the last line is incomplete
822 if (!hlines.empty()) {
823 // this does not work in LaTeX, so we ignore it
824 cerr << "Ignoring '" << hlines << "' at end of tabular"
830 void handle_hline_above(RowInfo & ri, vector<CellInfo> & ci)
833 for (size_t col = 0; col < ci.size(); ++col)
834 ci[col].topline = true;
838 void handle_hline_below(RowInfo & ri, vector<CellInfo> & ci)
840 ri.bottomline = true;
841 for (size_t col = 0; col < ci.size(); ++col)
842 ci[col].bottomline = true;
846 } // anonymous namespace
849 void handle_tabular(Parser & p, ostream & os, string const & name,
850 string const & tabularwidth, Context & context)
852 bool const is_long_tabular(name == "longtable");
853 bool booktabs = false;
854 string tabularvalignment("middle");
855 string posopts = p.getOpt();
856 if (!posopts.empty()) {
857 // FIXME: Convert this to ERT
859 cerr << "horizontal longtable positioning '"
860 << posopts << "' ignored\n";
861 else if (posopts == "[t]")
862 tabularvalignment = "top";
863 else if (posopts == "[b]")
864 tabularvalignment = "bottom";
866 cerr << "vertical tabular positioning '"
867 << posopts << "' ignored\n";
870 vector<ColInfo> colinfo;
872 // handle column formatting
873 handle_colalign(p, colinfo, ColInfo());
874 fix_colalign(colinfo);
876 // first scan of cells
877 // use table mode to keep it minimal-invasive
878 // not exactly what's TeX doing...
879 vector<string> lines;
881 RowPosition rowpos = ROW_START;
882 parse_table(p, ss, is_long_tabular, rowpos, FLAG_END);
883 split(ss.str(), lines, LINE);
885 vector< vector<CellInfo> > cellinfo(lines.size());
886 vector<RowInfo> rowinfo(lines.size());
893 //cerr << "// split into rows\n";
894 for (size_t row = 0; row < rowinfo.size();) {
897 cellinfo[row].resize(colinfo.size());
898 bool deletelastrow = false;
901 vector<string> dummy;
902 //cerr << "\n########### LINE: " << lines[row] << "########\n";
903 split(lines[row], dummy, HLINE);
905 // handle horizontal line fragments
906 // we do only expect this for a last line without '\\'
907 if (dummy.size() != 3) {
908 if ((dummy.size() != 1 && dummy.size() != 2) ||
909 row != rowinfo.size() - 1)
910 cerr << "unexpected dummy size: " << dummy.size()
911 << " content: " << lines[row] << "\n";
914 lines[row] = dummy[1];
916 //cerr << "line: " << row << " above 0: " << dummy[0] << "\n";
917 //cerr << "line: " << row << " below 2: " << dummy[2] << "\n";
918 //cerr << "line: " << row << " cells 1: " << dummy[1] << "\n";
920 for (int i = 0; i <= 2; i += 2) {
921 //cerr << " reading from line string '" << dummy[i] << "'\n";
924 Token t = p1.get_token();
925 //cerr << "read token: " << t << "\n";
926 if (t.cs() == "hline" || t.cs() == "toprule" ||
927 t.cs() == "midrule" ||
928 t.cs() == "bottomrule") {
929 if (t.cs() != "hline")
932 if (rowinfo[row].topline) {
933 if (row > 0) // extra bottomline above
934 handle_hline_below(rowinfo[row - 1], cellinfo[row - 1]);
936 cerr << "dropping extra "
938 //cerr << "below row: " << row-1 << endl;
940 handle_hline_above(rowinfo[row], cellinfo[row]);
941 //cerr << "above row: " << row << endl;
944 //cerr << "below row: " << row << endl;
945 handle_hline_below(rowinfo[row], cellinfo[row]);
947 } else if (t.cs() == "cline" || t.cs() == "cmidrule") {
948 if (t.cs() == "cmidrule")
950 string arg = p1.verbatim_item();
951 //cerr << "read " << t.cs() << " arg: '" << arg << "'\n";
953 split(arg, cols, '-');
955 size_t from = convert<unsigned int>(cols[0]);
957 cerr << "Could not parse "
958 << t.cs() << " start column."
961 // 1 based index -> 0 based
963 if (from >= colinfo.size()) {
964 cerr << t.cs() << " starts at "
965 "non existing column "
966 << (from + 1) << endl;
967 from = colinfo.size() - 1;
969 size_t to = convert<unsigned int>(cols[1]);
971 cerr << "Could not parse "
972 << t.cs() << " end column."
975 // 1 based index -> 0 based
977 if (to >= colinfo.size()) {
978 cerr << t.cs() << " ends at "
979 "non existing column "
981 to = colinfo.size() - 1;
983 for (size_t col = from; col <= to; ++col) {
984 //cerr << "row: " << row << " col: " << col << " i: " << i << endl;
986 rowinfo[row].topline = true;
987 cellinfo[row][col].topline = true;
989 rowinfo[row].bottomline = true;
990 cellinfo[row][col].bottomline = true;
993 } else if (t.cs() == "addlinespace") {
995 string const opt = p.next_token().cat() == catBegin ?
996 p.verbatim_item() : string();
999 rowinfo[row].top_space = "default";
1001 rowinfo[row].top_space = translate_len(opt);
1002 } else if (rowinfo[row].bottomline) {
1004 rowinfo[row].bottom_space = "default";
1006 rowinfo[row].bottom_space = translate_len(opt);
1009 rowinfo[row].interline_space = "default";
1011 rowinfo[row].interline_space = translate_len(opt);
1013 } else if (t.cs() == "endhead") {
1015 endhead.empty = true;
1017 rowinfo[row].type = LT_HEAD;
1018 for (int r = row - 1; r >= 0; --r) {
1019 if (rowinfo[r].type != LT_NORMAL)
1021 rowinfo[r].type = LT_HEAD;
1022 endhead.empty = false;
1025 } else if (t.cs() == "endfirsthead") {
1027 endfirsthead.empty = true;
1029 rowinfo[row].type = LT_FIRSTHEAD;
1030 for (int r = row - 1; r >= 0; --r) {
1031 if (rowinfo[r].type != LT_NORMAL)
1033 rowinfo[r].type = LT_FIRSTHEAD;
1034 endfirsthead.empty = false;
1036 endfirsthead.set = true;
1037 } else if (t.cs() == "endfoot") {
1039 endfoot.empty = true;
1041 rowinfo[row].type = LT_FOOT;
1042 for (int r = row - 1; r >= 0; --r) {
1043 if (rowinfo[r].type != LT_NORMAL)
1045 rowinfo[r].type = LT_FOOT;
1046 endfoot.empty = false;
1049 } else if (t.cs() == "endlastfoot") {
1051 endlastfoot.empty = true;
1053 rowinfo[row].type = LT_LASTFOOT;
1054 for (int r = row - 1; r >= 0; --r) {
1055 if (rowinfo[r].type != LT_NORMAL)
1057 rowinfo[r].type = LT_LASTFOOT;
1058 endlastfoot.empty = false;
1060 endlastfoot.set = true;
1061 } else if (t.cs() == "newpage") {
1064 rowinfo[row - 1].newpage = true;
1066 // This does not work in LaTeX
1072 rowinfo[row].newpage = true;
1074 cerr << "unexpected line token: " << t << endl;
1079 // LyX ends headers and footers always with \tabularnewline.
1080 // This causes one additional row in the output.
1081 // If the last row of a header/footer is empty, we can work
1082 // around that by removing it.
1084 RowInfo test = rowinfo[row-1];
1085 test.type = LT_NORMAL;
1086 if (lines[row-1].empty() && !test.special()) {
1087 switch (rowinfo[row-1].type) {
1089 if (rowinfo[row].type != LT_FIRSTHEAD &&
1090 rowinfo[row-2].type == LT_FIRSTHEAD)
1091 deletelastrow = true;
1094 if (rowinfo[row].type != LT_HEAD &&
1095 rowinfo[row-2].type == LT_HEAD)
1096 deletelastrow = true;
1099 if (rowinfo[row].type != LT_FOOT &&
1100 rowinfo[row-2].type == LT_FOOT)
1101 deletelastrow = true;
1104 if (rowinfo[row].type != LT_LASTFOOT &&
1105 rowinfo[row-2].type == LT_LASTFOOT)
1106 deletelastrow = true;
1114 if (deletelastrow) {
1115 lines.erase(lines.begin() + (row - 1));
1116 rowinfo.erase(rowinfo.begin() + (row - 1));
1117 cellinfo.erase(cellinfo.begin() + (row - 1));
1122 vector<string> cells;
1123 split(lines[row], cells, TAB);
1124 for (size_t col = 0, cell = 0; cell < cells.size();
1126 //cerr << "cell content: '" << cells[cell] << "'\n";
1127 if (col >= colinfo.size()) {
1128 // This does not work in LaTeX
1129 cerr << "Ignoring extra cell '"
1130 << cells[cell] << "'." << endl;
1133 Parser p(cells[cell]);
1135 //cells[cell] << "'\n";
1136 if (p.next_token().cs() == "multirow") {
1137 // We do not support the vpos arg yet.
1139 string const vpos = p.getArg('[', ']');
1140 p.skip_spaces(true);
1141 cerr << "Ignoring multirow's vpos arg '"
1142 << vpos << "'!" << endl;
1146 size_t const ncells =
1147 convert<unsigned int>(p.verbatim_item());
1148 // We do not support the bigstrut arg yet.
1150 string const bs = p.getArg('[', ']');
1151 p.skip_spaces(true);
1152 cerr << "Ignoring multirow's bigstrut arg '"
1153 << bs << "'!" << endl;
1155 // the width argument
1156 string const width = p.getArg('{', '}');
1160 vmove = p.getArg('[', ']');
1161 p.skip_spaces(true);
1165 colinfo[col].width = width;
1167 cellinfo[row][col].mroffset = vmove;
1168 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTIROW;
1169 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1170 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1171 cellinfo[row][col].mrxnum = ncells - 1;
1174 parse_text_in_inset(p, os2, FLAG_ITEM, false, context);
1175 if (!cellinfo[row][col].content.empty()) {
1176 // This may or may not work in LaTeX,
1177 // but it does not work in LyX.
1178 // FIXME: Handle it correctly!
1179 cerr << "Moving cell content '"
1181 << "' into a multirow cell. "
1182 "This will probably not work."
1185 cellinfo[row][col].content += os2.str();
1186 } else if (p.next_token().cs() == "multicolumn") {
1189 size_t const ncells =
1190 convert<unsigned int>(p.verbatim_item());
1192 // special cell properties alignment
1194 handle_colalign(p, t, ColInfo());
1195 p.skip_spaces(true);
1196 ColInfo & ci = t.front();
1198 // The logic of LyX for multicolumn vertical
1199 // lines is too complicated to reproduce it
1200 // here (see LyXTabular::TeXCellPreamble()).
1201 // Therefore we simply put everything in the
1205 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1206 cellinfo[row][col].align = ci.align;
1207 cellinfo[row][col].special = ci.special;
1208 cellinfo[row][col].leftlines = ci.leftlines;
1209 cellinfo[row][col].rightlines = ci.rightlines;
1211 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
1212 if (!cellinfo[row][col].content.empty()) {
1213 // This may or may not work in LaTeX,
1214 // but it does not work in LyX.
1215 // FIXME: Handle it correctly!
1216 cerr << "Moving cell content '"
1218 << "' into a multicolumn cell. "
1219 "This will probably not work."
1222 cellinfo[row][col].content += os.str();
1224 // add dummy cells for multicol
1225 for (size_t i = 0; i < ncells - 1; ++i) {
1227 if (col >= colinfo.size()) {
1228 cerr << "The cell '"
1231 << convert<string>(ncells)
1232 << " columns while the table has only "
1233 << convert<string>(colinfo.size())
1235 << " Therefore the surplus columns will be ignored."
1239 cellinfo[row][col].multi = CELL_PART_OF_MULTICOLUMN;
1240 cellinfo[row][col].align = 'c';
1243 } else if (col == 0 && colinfo.size() > 1 &&
1245 p.next_token().cs() == "caption") {
1246 // longtable caption support in LyX is a hack:
1247 // Captions require a row of their own with
1248 // the caption flag set to true, having only
1249 // one multicolumn cell. The contents of that
1250 // cell must contain exactly one caption inset
1251 // and nothing else.
1252 // Fortunately, the caption flag is only needed
1253 // for tables with more than one column.
1254 rowinfo[row].caption = true;
1255 for (size_t c = 1; c < cells.size(); ++c) {
1256 if (!cells[c].empty()) {
1257 cerr << "Moving cell content '"
1259 << "' into the caption cell. "
1260 "This will probably not work."
1262 cells[0] += cells[c];
1266 cellinfo[row][col].align = colinfo[col].align;
1267 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1269 parse_text_in_inset(p, os, FLAG_CELL, false, context);
1270 cellinfo[row][col].content += os.str();
1271 // add dummy multicolumn cells
1272 for (size_t c = 1; c < colinfo.size(); ++c)
1273 cellinfo[row][c].multi = CELL_PART_OF_MULTICOLUMN;
1277 if (p.next_token().cs() == "begin") {
1280 string const env = p.getArg('{', '}');
1281 if (env == "sideways" || env == "turn") {
1282 string angle = "90";
1283 if (env == "turn") {
1285 angle = p.getArg('{', '}');
1287 active_environments.push_back(env);
1288 p.ertEnvironment(env);
1289 active_environments.pop_back();
1291 if (!p.good() && support::isStrInt(angle))
1292 rotate = convert<int>(angle);
1296 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1297 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1298 cellinfo[row][col].align = colinfo[col].align;
1301 cellinfo[row][col].rotate = rotate;
1303 active_environments.push_back(p.getArg('{', '}'));
1306 parse_text_in_inset(p, os, FLAG_END, false, context);
1307 active_environments.pop_back();
1308 preamble.registerAutomaticallyLoadedPackage("rotating");
1310 parse_text_in_inset(p, os, FLAG_CELL, false, context);
1312 cellinfo[row][col].content += os.str();
1316 //cerr << "// handle almost empty last row what we have\n";
1317 // handle almost empty last row
1318 if (row && lines[row].empty() && row + 1 == rowinfo.size()) {
1319 //cerr << "remove empty last line\n";
1320 if (rowinfo[row].topline)
1321 rowinfo[row - 1].bottomline = true;
1322 for (size_t col = 0; col < colinfo.size(); ++col)
1323 if (cellinfo[row][col].topline)
1324 cellinfo[row - 1][col].bottomline = true;
1331 // Now we have the table structure and content in rowinfo, colinfo
1333 // Unfortunately LyX has some limitations that we need to work around.
1336 for (size_t row = 0; row < rowinfo.size(); ++row) {
1337 for (size_t col = 0; col < cellinfo[row].size(); ++col) {
1338 // Convert cells with special content to multicolumn cells
1339 // (LyX ignores the special field for non-multicolumn cells).
1340 if (cellinfo[row][col].multi == CELL_NORMAL &&
1341 !cellinfo[row][col].special.empty())
1342 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1343 // Add multirow dummy cells
1344 if (row > 1 && (cellinfo[row - 1][col].multi == CELL_PART_OF_MULTIROW
1345 || cellinfo[row - 1][col].multi == CELL_BEGIN_OF_MULTIROW)
1346 && cellinfo[row - 1][col].mrxnum > 0) {
1347 // add dummy cells for multirow
1348 cellinfo[row][col].multi = CELL_PART_OF_MULTIROW;
1349 cellinfo[row][col].align = 'c';
1350 cellinfo[row][col].mrxnum = cellinfo[row - 1][col].mrxnum - 1;
1355 // Distribute lines from rows/columns to cells
1356 // The code was stolen from convert_tablines() in lyx2lyx/lyx_1_6.py.
1357 // Each standard cell inherits the settings of the corresponding
1358 // rowinfo/colinfo. This works because all cells with individual
1359 // settings were converted to multicolumn cells above.
1360 // Each multicolumn cell inherits the settings of the rowinfo/colinfo
1361 // corresponding to the first column of the multicolumn cell (default
1362 // of the multicol package). This works because the special field
1363 // overrides the line fields.
1364 for (size_t row = 0; row < rowinfo.size(); ++row) {
1365 for (size_t col = 0; col < cellinfo[row].size(); ++col) {
1366 if (cellinfo[row][col].multi == CELL_NORMAL) {
1367 cellinfo[row][col].topline = rowinfo[row].topline;
1368 cellinfo[row][col].bottomline = rowinfo[row].bottomline;
1369 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1370 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1371 } else if (cellinfo[row][col].multi == CELL_BEGIN_OF_MULTICOLUMN) {
1373 while (s < cellinfo[row].size() &&
1374 cellinfo[row][s].multi == CELL_PART_OF_MULTICOLUMN)
1376 if (s < cellinfo[row].size() &&
1377 cellinfo[row][s].multi != CELL_BEGIN_OF_MULTICOLUMN)
1378 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1379 if (col > 0 && cellinfo[row][col-1].multi == CELL_NORMAL)
1380 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1381 } else if (cellinfo[row][col].multi == CELL_BEGIN_OF_MULTIROW) {
1383 while (s < rowinfo.size() &&
1384 cellinfo[s][col].multi == CELL_PART_OF_MULTIROW)
1386 if (s < cellinfo[row].size() &&
1387 cellinfo[s][col].multi != CELL_BEGIN_OF_MULTIROW)
1388 cellinfo[row][col].bottomline = rowinfo[row].bottomline;
1389 if (row > 0 && cellinfo[row - 1][col].multi == CELL_NORMAL)
1390 cellinfo[row][col].topline = rowinfo[row].topline;
1396 preamble.registerAutomaticallyLoadedPackage("booktabs");
1397 if (is_long_tabular)
1398 preamble.registerAutomaticallyLoadedPackage("longtable");
1400 //cerr << "// output what we have\n";
1401 // output what we have
1402 string const rotate = "0";
1403 os << "\n<lyxtabular version=\"3\" rows=\"" << rowinfo.size()
1404 << "\" columns=\"" << colinfo.size() << "\">\n";
1406 << write_attribute("rotate", rotate)
1407 << write_attribute("booktabs", booktabs)
1408 << write_attribute("islongtable", is_long_tabular);
1409 if (is_long_tabular) {
1410 os << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1411 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1412 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1413 << write_attribute("headTopDL", endhead.topDL)
1414 << write_attribute("headBottomDL", endhead.bottomDL)
1415 << write_attribute("footTopDL", endfoot.topDL)
1416 << write_attribute("footBottomDL", endfoot.bottomDL)
1417 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1418 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1419 << write_attribute("lastFootEmpty", endlastfoot.empty);
1421 os << write_attribute("tabularvalignment", tabularvalignment)
1422 << write_attribute("tabularwidth", tabularwidth);
1425 //cerr << "// after header\n";
1426 for (size_t col = 0; col < colinfo.size(); ++col) {
1427 os << "<column alignment=\""
1428 << verbose_align(colinfo[col].align) << "\""
1430 << verbose_valign(colinfo[col].valign) << "\""
1431 << write_attribute("width", translate_len(colinfo[col].width))
1432 << write_attribute("special", colinfo[col].special)
1435 //cerr << "// after cols\n";
1437 for (size_t row = 0; row < rowinfo.size(); ++row) {
1439 << write_attribute("topspace", rowinfo[row].top_space)
1440 << write_attribute("bottomspace", rowinfo[row].bottom_space)
1441 << write_attribute("interlinespace", rowinfo[row].interline_space)
1442 << write_attribute("endhead",
1443 rowinfo[row].type == LT_HEAD)
1444 << write_attribute("endfirsthead",
1445 rowinfo[row].type == LT_FIRSTHEAD)
1446 << write_attribute("endfoot",
1447 rowinfo[row].type == LT_FOOT)
1448 << write_attribute("endlastfoot",
1449 rowinfo[row].type == LT_LASTFOOT)
1450 << write_attribute("newpage", rowinfo[row].newpage)
1451 << write_attribute("caption", rowinfo[row].caption)
1453 for (size_t col = 0; col < colinfo.size(); ++col) {
1454 CellInfo const & cell = cellinfo[row][col];
1456 if (cell.multi == CELL_BEGIN_OF_MULTICOLUMN
1457 || cell.multi == CELL_PART_OF_MULTICOLUMN)
1458 os << " multicolumn=\"" << cell.multi << "\"";
1459 if (cell.multi == CELL_BEGIN_OF_MULTIROW
1460 || cell.multi == CELL_PART_OF_MULTIROW)
1461 os << " multirow=\"" << cell.multi << "\"";
1462 os << " alignment=\"" << verbose_align(cell.align)
1464 << " valignment=\"" << verbose_valign(cell.valign)
1466 << write_attribute("topline", cell.topline)
1467 << write_attribute("bottomline", cell.bottomline)
1468 << write_attribute("leftline", cell.leftlines > 0)
1469 << write_attribute("rightline", cell.rightlines > 0)
1470 << write_attribute("rotate", cell.rotate)
1471 << write_attribute("mroffset", cell.mroffset);
1472 //cerr << "\nrow: " << row << " col: " << col;
1474 // cerr << " topline=\"true\"";
1475 //if (cell.bottomline)
1476 // cerr << " bottomline=\"true\"";
1477 os << " usebox=\"none\""
1478 << write_attribute("width", translate_len(cell.width));
1479 if (cell.multi != CELL_NORMAL)
1480 os << write_attribute("special", cell.special);
1482 << "\n\\begin_inset Text\n"
1484 << "\n\\end_inset\n"
1490 os << "</lyxtabular>\n";