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, string const & halign,
853 bool const is_long_tabular(name == "longtable");
854 bool booktabs = false;
855 string tabularvalignment("middle");
856 string posopts = p.getOpt();
857 if (!posopts.empty()) {
858 // FIXME: Convert this to ERT
860 cerr << "horizontal longtable positioning '"
861 << posopts << "' ignored\n";
862 else if (posopts == "[t]")
863 tabularvalignment = "top";
864 else if (posopts == "[b]")
865 tabularvalignment = "bottom";
867 cerr << "vertical tabular positioning '"
868 << posopts << "' ignored\n";
871 vector<ColInfo> colinfo;
873 // handle column formatting
874 handle_colalign(p, colinfo, ColInfo());
875 fix_colalign(colinfo);
877 // first scan of cells
878 // use table mode to keep it minimal-invasive
879 // not exactly what's TeX doing...
880 vector<string> lines;
882 RowPosition rowpos = ROW_START;
883 parse_table(p, ss, is_long_tabular, rowpos, FLAG_END);
884 split(ss.str(), lines, LINE);
886 vector< vector<CellInfo> > cellinfo(lines.size());
887 vector<RowInfo> rowinfo(lines.size());
894 //cerr << "// split into rows\n";
895 for (size_t row = 0; row < rowinfo.size();) {
898 cellinfo[row].resize(colinfo.size());
899 bool deletelastrow = false;
902 vector<string> dummy;
903 //cerr << "\n########### LINE: " << lines[row] << "########\n";
904 split(lines[row], dummy, HLINE);
906 // handle horizontal line fragments
907 // we do only expect this for a last line without '\\'
908 if (dummy.size() != 3) {
909 if ((dummy.size() != 1 && dummy.size() != 2) ||
910 row != rowinfo.size() - 1)
911 cerr << "unexpected dummy size: " << dummy.size()
912 << " content: " << lines[row] << "\n";
915 lines[row] = dummy[1];
917 //cerr << "line: " << row << " above 0: " << dummy[0] << "\n";
918 //cerr << "line: " << row << " below 2: " << dummy[2] << "\n";
919 //cerr << "line: " << row << " cells 1: " << dummy[1] << "\n";
921 for (int i = 0; i <= 2; i += 2) {
922 //cerr << " reading from line string '" << dummy[i] << "'\n";
925 Token t = p1.get_token();
926 //cerr << "read token: " << t << "\n";
927 if (t.cs() == "hline" || t.cs() == "toprule" ||
928 t.cs() == "midrule" ||
929 t.cs() == "bottomrule") {
930 if (t.cs() != "hline")
933 if (rowinfo[row].topline) {
934 if (row > 0) // extra bottomline above
935 handle_hline_below(rowinfo[row - 1], cellinfo[row - 1]);
937 cerr << "dropping extra "
939 //cerr << "below row: " << row-1 << endl;
941 handle_hline_above(rowinfo[row], cellinfo[row]);
942 //cerr << "above row: " << row << endl;
945 //cerr << "below row: " << row << endl;
946 handle_hline_below(rowinfo[row], cellinfo[row]);
948 } else if (t.cs() == "cline" || t.cs() == "cmidrule") {
949 if (t.cs() == "cmidrule")
951 string arg = p1.verbatim_item();
952 //cerr << "read " << t.cs() << " arg: '" << arg << "'\n";
954 split(arg, cols, '-');
956 size_t from = convert<unsigned int>(cols[0]);
958 cerr << "Could not parse "
959 << t.cs() << " start column."
962 // 1 based index -> 0 based
964 if (from >= colinfo.size()) {
965 cerr << t.cs() << " starts at "
966 "non existing column "
967 << (from + 1) << endl;
968 from = colinfo.size() - 1;
970 size_t to = convert<unsigned int>(cols[1]);
972 cerr << "Could not parse "
973 << t.cs() << " end column."
976 // 1 based index -> 0 based
978 if (to >= colinfo.size()) {
979 cerr << t.cs() << " ends at "
980 "non existing column "
982 to = colinfo.size() - 1;
984 for (size_t col = from; col <= to; ++col) {
985 //cerr << "row: " << row << " col: " << col << " i: " << i << endl;
987 rowinfo[row].topline = true;
988 cellinfo[row][col].topline = true;
990 rowinfo[row].bottomline = true;
991 cellinfo[row][col].bottomline = true;
994 } else if (t.cs() == "addlinespace") {
996 string const opt = p.next_token().cat() == catBegin ?
997 p.verbatim_item() : string();
1000 rowinfo[row].top_space = "default";
1002 rowinfo[row].top_space = translate_len(opt);
1003 } else if (rowinfo[row].bottomline) {
1005 rowinfo[row].bottom_space = "default";
1007 rowinfo[row].bottom_space = translate_len(opt);
1010 rowinfo[row].interline_space = "default";
1012 rowinfo[row].interline_space = translate_len(opt);
1014 } else if (t.cs() == "endhead") {
1016 endhead.empty = true;
1018 rowinfo[row].type = LT_HEAD;
1019 for (int r = row - 1; r >= 0; --r) {
1020 if (rowinfo[r].type != LT_NORMAL)
1022 rowinfo[r].type = LT_HEAD;
1023 endhead.empty = false;
1026 } else if (t.cs() == "endfirsthead") {
1028 endfirsthead.empty = true;
1030 rowinfo[row].type = LT_FIRSTHEAD;
1031 for (int r = row - 1; r >= 0; --r) {
1032 if (rowinfo[r].type != LT_NORMAL)
1034 rowinfo[r].type = LT_FIRSTHEAD;
1035 endfirsthead.empty = false;
1037 endfirsthead.set = true;
1038 } else if (t.cs() == "endfoot") {
1040 endfoot.empty = true;
1042 rowinfo[row].type = LT_FOOT;
1043 for (int r = row - 1; r >= 0; --r) {
1044 if (rowinfo[r].type != LT_NORMAL)
1046 rowinfo[r].type = LT_FOOT;
1047 endfoot.empty = false;
1050 } else if (t.cs() == "endlastfoot") {
1052 endlastfoot.empty = true;
1054 rowinfo[row].type = LT_LASTFOOT;
1055 for (int r = row - 1; r >= 0; --r) {
1056 if (rowinfo[r].type != LT_NORMAL)
1058 rowinfo[r].type = LT_LASTFOOT;
1059 endlastfoot.empty = false;
1061 endlastfoot.set = true;
1062 } else if (t.cs() == "newpage") {
1065 rowinfo[row - 1].newpage = true;
1067 // This does not work in LaTeX
1073 rowinfo[row].newpage = true;
1075 cerr << "unexpected line token: " << t << endl;
1080 // LyX ends headers and footers always with \tabularnewline.
1081 // This causes one additional row in the output.
1082 // If the last row of a header/footer is empty, we can work
1083 // around that by removing it.
1085 RowInfo test = rowinfo[row-1];
1086 test.type = LT_NORMAL;
1087 if (lines[row-1].empty() && !test.special()) {
1088 switch (rowinfo[row-1].type) {
1090 if (rowinfo[row].type != LT_FIRSTHEAD &&
1091 rowinfo[row-2].type == LT_FIRSTHEAD)
1092 deletelastrow = true;
1095 if (rowinfo[row].type != LT_HEAD &&
1096 rowinfo[row-2].type == LT_HEAD)
1097 deletelastrow = true;
1100 if (rowinfo[row].type != LT_FOOT &&
1101 rowinfo[row-2].type == LT_FOOT)
1102 deletelastrow = true;
1105 if (rowinfo[row].type != LT_LASTFOOT &&
1106 rowinfo[row-2].type == LT_LASTFOOT)
1107 deletelastrow = true;
1115 if (deletelastrow) {
1116 lines.erase(lines.begin() + (row - 1));
1117 rowinfo.erase(rowinfo.begin() + (row - 1));
1118 cellinfo.erase(cellinfo.begin() + (row - 1));
1123 vector<string> cells;
1124 split(lines[row], cells, TAB);
1125 for (size_t col = 0, cell = 0; cell < cells.size();
1127 //cerr << "cell content: '" << cells[cell] << "'\n";
1128 if (col >= colinfo.size()) {
1129 // This does not work in LaTeX
1130 cerr << "Ignoring extra cell '"
1131 << cells[cell] << "'." << endl;
1134 Parser p(cells[cell]);
1136 //cells[cell] << "'\n";
1137 if (p.next_token().cs() == "multirow") {
1138 // We do not support the vpos arg yet.
1140 string const vpos = p.getArg('[', ']');
1141 p.skip_spaces(true);
1142 cerr << "Ignoring multirow's vpos arg '"
1143 << vpos << "'!" << endl;
1147 size_t const ncells =
1148 convert<unsigned int>(p.verbatim_item());
1149 // We do not support the bigstrut arg yet.
1151 string const bs = p.getArg('[', ']');
1152 p.skip_spaces(true);
1153 cerr << "Ignoring multirow's bigstrut arg '"
1154 << bs << "'!" << endl;
1156 // the width argument
1157 string const width = p.getArg('{', '}');
1161 vmove = p.getArg('[', ']');
1162 p.skip_spaces(true);
1166 colinfo[col].width = width;
1168 cellinfo[row][col].mroffset = vmove;
1169 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTIROW;
1170 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1171 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1172 cellinfo[row][col].mrxnum = ncells - 1;
1175 parse_text_in_inset(p, os2, FLAG_ITEM, false, context);
1176 if (!cellinfo[row][col].content.empty()) {
1177 // This may or may not work in LaTeX,
1178 // but it does not work in LyX.
1179 // FIXME: Handle it correctly!
1180 cerr << "Moving cell content '"
1182 << "' into a multirow cell. "
1183 "This will probably not work."
1186 cellinfo[row][col].content += os2.str();
1187 } else if (p.next_token().cs() == "multicolumn") {
1190 size_t const ncells =
1191 convert<unsigned int>(p.verbatim_item());
1193 // special cell properties alignment
1195 handle_colalign(p, t, ColInfo());
1196 p.skip_spaces(true);
1197 ColInfo & ci = t.front();
1199 // The logic of LyX for multicolumn vertical
1200 // lines is too complicated to reproduce it
1201 // here (see LyXTabular::TeXCellPreamble()).
1202 // Therefore we simply put everything in the
1206 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1207 cellinfo[row][col].align = ci.align;
1208 cellinfo[row][col].special = ci.special;
1209 cellinfo[row][col].leftlines = ci.leftlines;
1210 cellinfo[row][col].rightlines = ci.rightlines;
1212 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
1213 if (!cellinfo[row][col].content.empty()) {
1214 // This may or may not work in LaTeX,
1215 // but it does not work in LyX.
1216 // FIXME: Handle it correctly!
1217 cerr << "Moving cell content '"
1219 << "' into a multicolumn cell. "
1220 "This will probably not work."
1223 cellinfo[row][col].content += os.str();
1225 // add dummy cells for multicol
1226 for (size_t i = 0; i < ncells - 1; ++i) {
1228 if (col >= colinfo.size()) {
1229 cerr << "The cell '"
1232 << convert<string>(ncells)
1233 << " columns while the table has only "
1234 << convert<string>(colinfo.size())
1236 << " Therefore the surplus columns will be ignored."
1240 cellinfo[row][col].multi = CELL_PART_OF_MULTICOLUMN;
1241 cellinfo[row][col].align = 'c';
1244 } else if (col == 0 && colinfo.size() > 1 &&
1246 p.next_token().cs() == "caption") {
1247 // longtable caption support in LyX is a hack:
1248 // Captions require a row of their own with
1249 // the caption flag set to true, having only
1250 // one multicolumn cell. The contents of that
1251 // cell must contain exactly one caption inset
1252 // and nothing else.
1253 // Fortunately, the caption flag is only needed
1254 // for tables with more than one column.
1255 rowinfo[row].caption = true;
1256 for (size_t c = 1; c < cells.size(); ++c) {
1257 if (!cells[c].empty()) {
1258 cerr << "Moving cell content '"
1260 << "' into the caption cell. "
1261 "This will probably not work."
1263 cells[0] += cells[c];
1267 cellinfo[row][col].align = colinfo[col].align;
1268 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1270 parse_text_in_inset(p, os, FLAG_CELL, false, context);
1271 cellinfo[row][col].content += os.str();
1272 // add dummy multicolumn cells
1273 for (size_t c = 1; c < colinfo.size(); ++c)
1274 cellinfo[row][c].multi = CELL_PART_OF_MULTICOLUMN;
1278 if (p.next_token().cs() == "begin") {
1281 string const env = p.getArg('{', '}');
1282 if (env == "sideways" || env == "turn") {
1283 string angle = "90";
1284 if (env == "turn") {
1286 angle = p.getArg('{', '}');
1288 active_environments.push_back(env);
1289 p.ertEnvironment(env);
1290 active_environments.pop_back();
1292 if (!p.good() && support::isStrInt(angle))
1293 rotate = convert<int>(angle);
1297 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1298 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1299 cellinfo[row][col].align = colinfo[col].align;
1302 cellinfo[row][col].rotate = rotate;
1304 active_environments.push_back(p.getArg('{', '}'));
1307 parse_text_in_inset(p, os, FLAG_END, false, context);
1308 active_environments.pop_back();
1309 preamble.registerAutomaticallyLoadedPackage("rotating");
1311 parse_text_in_inset(p, os, FLAG_CELL, false, context);
1313 cellinfo[row][col].content += os.str();
1317 //cerr << "// handle almost empty last row what we have\n";
1318 // handle almost empty last row
1319 if (row && lines[row].empty() && row + 1 == rowinfo.size()) {
1320 //cerr << "remove empty last line\n";
1321 if (rowinfo[row].topline)
1322 rowinfo[row - 1].bottomline = true;
1323 for (size_t col = 0; col < colinfo.size(); ++col)
1324 if (cellinfo[row][col].topline)
1325 cellinfo[row - 1][col].bottomline = true;
1332 // Now we have the table structure and content in rowinfo, colinfo
1334 // Unfortunately LyX has some limitations that we need to work around.
1337 for (size_t row = 0; row < rowinfo.size(); ++row) {
1338 for (size_t col = 0; col < cellinfo[row].size(); ++col) {
1339 // Convert cells with special content to multicolumn cells
1340 // (LyX ignores the special field for non-multicolumn cells).
1341 if (cellinfo[row][col].multi == CELL_NORMAL &&
1342 !cellinfo[row][col].special.empty())
1343 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1344 // Add multirow dummy cells
1345 if (row > 1 && (cellinfo[row - 1][col].multi == CELL_PART_OF_MULTIROW
1346 || cellinfo[row - 1][col].multi == CELL_BEGIN_OF_MULTIROW)
1347 && cellinfo[row - 1][col].mrxnum > 0) {
1348 // add dummy cells for multirow
1349 cellinfo[row][col].multi = CELL_PART_OF_MULTIROW;
1350 cellinfo[row][col].align = 'c';
1351 cellinfo[row][col].mrxnum = cellinfo[row - 1][col].mrxnum - 1;
1356 // Distribute lines from rows/columns to cells
1357 // The code was stolen from convert_tablines() in lyx2lyx/lyx_1_6.py.
1358 // Each standard cell inherits the settings of the corresponding
1359 // rowinfo/colinfo. This works because all cells with individual
1360 // settings were converted to multicolumn cells above.
1361 // Each multicolumn cell inherits the settings of the rowinfo/colinfo
1362 // corresponding to the first column of the multicolumn cell (default
1363 // of the multicol package). This works because the special field
1364 // overrides the line fields.
1365 for (size_t row = 0; row < rowinfo.size(); ++row) {
1366 for (size_t col = 0; col < cellinfo[row].size(); ++col) {
1367 if (cellinfo[row][col].multi == CELL_NORMAL) {
1368 cellinfo[row][col].topline = rowinfo[row].topline;
1369 cellinfo[row][col].bottomline = rowinfo[row].bottomline;
1370 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1371 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1372 } else if (cellinfo[row][col].multi == CELL_BEGIN_OF_MULTICOLUMN) {
1374 while (s < cellinfo[row].size() &&
1375 cellinfo[row][s].multi == CELL_PART_OF_MULTICOLUMN)
1377 if (s < cellinfo[row].size() &&
1378 cellinfo[row][s].multi != CELL_BEGIN_OF_MULTICOLUMN)
1379 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1380 if (col > 0 && cellinfo[row][col-1].multi == CELL_NORMAL)
1381 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1382 } else if (cellinfo[row][col].multi == CELL_BEGIN_OF_MULTIROW) {
1384 while (s < rowinfo.size() &&
1385 cellinfo[s][col].multi == CELL_PART_OF_MULTIROW)
1387 if (s < cellinfo[row].size() &&
1388 cellinfo[s][col].multi != CELL_BEGIN_OF_MULTIROW)
1389 cellinfo[row][col].bottomline = rowinfo[row].bottomline;
1390 if (row > 0 && cellinfo[row - 1][col].multi == CELL_NORMAL)
1391 cellinfo[row][col].topline = rowinfo[row].topline;
1397 preamble.registerAutomaticallyLoadedPackage("booktabs");
1398 if (is_long_tabular)
1399 preamble.registerAutomaticallyLoadedPackage("longtable");
1401 //cerr << "// output what we have\n";
1402 // output what we have
1403 string const rotate = "0";
1404 os << "\n<lyxtabular version=\"3\" rows=\"" << rowinfo.size()
1405 << "\" columns=\"" << colinfo.size() << "\">\n";
1407 << write_attribute("rotate", rotate)
1408 << write_attribute("booktabs", booktabs)
1409 << write_attribute("islongtable", is_long_tabular);
1410 if (is_long_tabular) {
1411 os << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1412 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1413 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1414 << write_attribute("headTopDL", endhead.topDL)
1415 << write_attribute("headBottomDL", endhead.bottomDL)
1416 << write_attribute("footTopDL", endfoot.topDL)
1417 << write_attribute("footBottomDL", endfoot.bottomDL)
1418 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1419 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1420 << write_attribute("lastFootEmpty", endlastfoot.empty);
1421 if (!halign.empty())
1422 os << write_attribute("longtabularalignment", halign);
1424 os << write_attribute("tabularvalignment", tabularvalignment)
1425 << write_attribute("tabularwidth", tabularwidth);
1428 //cerr << "// after header\n";
1429 for (size_t col = 0; col < colinfo.size(); ++col) {
1430 os << "<column alignment=\""
1431 << verbose_align(colinfo[col].align) << "\""
1433 << verbose_valign(colinfo[col].valign) << "\""
1434 << write_attribute("width", translate_len(colinfo[col].width))
1435 << write_attribute("special", colinfo[col].special)
1438 //cerr << "// after cols\n";
1440 for (size_t row = 0; row < rowinfo.size(); ++row) {
1442 << write_attribute("topspace", rowinfo[row].top_space)
1443 << write_attribute("bottomspace", rowinfo[row].bottom_space)
1444 << write_attribute("interlinespace", rowinfo[row].interline_space)
1445 << write_attribute("endhead",
1446 rowinfo[row].type == LT_HEAD)
1447 << write_attribute("endfirsthead",
1448 rowinfo[row].type == LT_FIRSTHEAD)
1449 << write_attribute("endfoot",
1450 rowinfo[row].type == LT_FOOT)
1451 << write_attribute("endlastfoot",
1452 rowinfo[row].type == LT_LASTFOOT)
1453 << write_attribute("newpage", rowinfo[row].newpage)
1454 << write_attribute("caption", rowinfo[row].caption)
1456 for (size_t col = 0; col < colinfo.size(); ++col) {
1457 CellInfo const & cell = cellinfo[row][col];
1459 if (cell.multi == CELL_BEGIN_OF_MULTICOLUMN
1460 || cell.multi == CELL_PART_OF_MULTICOLUMN)
1461 os << " multicolumn=\"" << cell.multi << "\"";
1462 if (cell.multi == CELL_BEGIN_OF_MULTIROW
1463 || cell.multi == CELL_PART_OF_MULTIROW)
1464 os << " multirow=\"" << cell.multi << "\"";
1465 os << " alignment=\"" << verbose_align(cell.align)
1467 << " valignment=\"" << verbose_valign(cell.valign)
1469 << write_attribute("topline", cell.topline)
1470 << write_attribute("bottomline", cell.bottomline)
1471 << write_attribute("leftline", cell.leftlines > 0)
1472 << write_attribute("rightline", cell.rightlines > 0)
1473 << write_attribute("rotate", cell.rotate)
1474 << write_attribute("mroffset", cell.mroffset);
1475 //cerr << "\nrow: " << row << " col: " << col;
1477 // cerr << " topline=\"true\"";
1478 //if (cell.bottomline)
1479 // cerr << " bottomline=\"true\"";
1480 os << " usebox=\"none\""
1481 << write_attribute("width", translate_len(cell.width));
1482 if (cell.multi != CELL_NORMAL)
1483 os << write_attribute("special", cell.special);
1485 << "\n\\begin_inset Text\n"
1487 << "\n\\end_inset\n"
1493 os << "</lyxtabular>\n";