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 = 1,
110 /// This is a dummy cell (part of a multicolumn cell)
111 CELL_PART_OF_MULTICOLUMN = 2
117 CellInfo() : multi(CELL_NORMAL), align('n'), valign('n'),
118 leftlines(0), rightlines(0), topline(false),
119 bottomline(false), rotate(0) {}
126 /// vertical cell alignment
128 /// number of lines on the left
130 /// number of lines on the right
132 /// do we have a line above?
134 /// do we have a line below?
136 /// how is the cell rotated?
138 /// width for multicolumn cells
140 /// special formatting for multicolumn cells
148 ltType() : set(false), topDL(false), bottomDL(false), empty(false) {}
149 // we have this header type (is set in the getLT... functions)
151 // double borders on top
153 // double borders on bottom
155 // used for FirstHeader & LastFooter and if this is true
156 // all the rows marked as FirstHeader or LastFooter are
157 // ignored in the output and it is set to be empty!
162 /// translate a horizontal alignment (as stored in ColInfo and CellInfo) to LyX
163 inline char const * verbose_align(char c)
178 /// translate a vertical alignment (as stored in ColInfo and CellInfo) to LyX
179 inline char const * verbose_valign(char c)
181 // The default value for no special alignment is "top".
194 // stripped down from tabluar.C. We use it currently only for bools and
196 string const write_attribute(string const & name, bool const & b)
198 // we write only true attribute values so we remove a bit of the
199 // file format bloat for tabulars.
200 return b ? ' ' + name + "=\"true\"" : string();
204 string const write_attribute(string const & name, string const & s)
206 return s.empty() ? string() : ' ' + name + "=\"" + s + '"';
210 string const write_attribute(string const & name, int const & i)
212 // we write only true attribute values so we remove a bit of the
213 // file format bloat for tabulars.
214 return i ? write_attribute(name, convert<string>(i)) : string();
218 /*! rather brutish way to code table structure in a string:
223 \multicolumn{2}{c}{4} & 5 //
229 gets "translated" to:
232 HLINE 1 TAB 2 TAB 3 HLINE HLINE LINE
233 \hline HLINE \multicolumn{2}{c}{4} TAB 5 HLINE HLINE LINE
234 HLINE 6 TAB 7 HLINE HLINE LINE
235 HLINE 8 HLINE \endhead HLINE LINE
239 char const TAB = '\001';
240 char const LINE = '\002';
241 char const HLINE = '\004';
245 * Move the information in leftlines, rightlines, align and valign to the
246 * special field. This is necessary if the special field is not empty,
247 * because LyX ignores leftlines > 1, rightlines > 1, align and valign in
250 void ci2special(ColInfo & ci)
252 if (ci.width.empty() && ci.align == 'n')
253 // The alignment setting is already in special, since
254 // handle_colalign() never stores ci with these settings
255 // and ensures that leftlines == 0 and rightlines == 0 in
259 if (!ci.width.empty()) {
262 ci.special += ">{\\raggedright}";
265 ci.special += ">{\\raggedleft}";
268 ci.special += ">{\\centering}";
271 if (ci.valign == 'n')
274 ci.special += ci.valign;
275 ci.special += '{' + ci.width + '}';
278 ci.special += ci.align;
280 // LyX can only have one left and one right line.
281 for (int i = 1; i < ci.leftlines; ++i)
282 ci.special.insert(0, "|");
283 for (int i = 1; i < ci.rightlines; ++i)
285 ci.leftlines = min(ci.leftlines, 1);
286 ci.rightlines = min(ci.rightlines, 1);
293 * Handle column specifications for tabulars and multicolumns.
294 * The next token of the parser \p p must be an opening brace, and we read
295 * everything until the matching closing brace.
296 * The resulting column specifications are filled into \p colinfo. This is
297 * in an intermediate form. fix_colalign() makes it suitable for LyX output.
299 void handle_colalign(Parser & p, vector<ColInfo> & colinfo,
300 ColInfo const & start)
302 if (p.get_token().cat() != catBegin)
303 cerr << "Wrong syntax for table column alignment.\n"
304 "Expected '{', got '" << p.curr_token().asInput()
307 ColInfo next = start;
308 for (Token t = p.get_token(); p.good() && t.cat() != catEnd;
311 cerr << "t: " << t << " c: '" << t.character() << "'\n";
314 // We cannot handle comments here
315 if (t.cat() == catComment) {
316 if (t.cs().empty()) {
320 cerr << "Ignoring comment: " << t.asInput();
324 switch (t.character()) {
326 // whitespace, ignore.
331 // new column, horizontal aligned
332 next.align = t.character();
333 if (!next.special.empty())
335 colinfo.push_back(next);
341 // new column, vertical aligned box
342 next.valign = t.character();
343 next.width = p.verbatim_item();
344 if (!next.special.empty())
346 colinfo.push_back(next);
351 if (colinfo.empty()) {
352 if (next.special.empty())
356 } else if (colinfo.back().special.empty())
357 ++colinfo.back().rightlines;
358 else if (next.special.empty())
361 colinfo.back().special += '|';
364 // text before the next column
365 string const s = trimSpaceAndEol(p.verbatim_item());
366 if (next.special.empty() &&
368 // Maybe this can be converted to a
369 // horizontal alignment setting for
370 // fixed width columns
371 if (s == "\\raggedleft")
373 else if (s == "\\raggedright")
375 else if (s == "\\centering")
378 next.special = ">{" + s + '}';
380 next.special += ">{" + s + '}';
384 // text after the last column
385 string const s = trimSpaceAndEol(p.verbatim_item());
387 // This is not possible in LaTeX.
388 cerr << "Ignoring separator '<{"
389 << s << "}'." << endl;
391 ColInfo & ci = colinfo.back();
393 ci.special += "<{" + s + '}';
398 // *{n}{arg} means 'n' columns of type 'arg'
399 string const num = p.verbatim_item();
400 string const arg = p.verbatim_item();
401 size_t const n = convert<unsigned int>(num);
402 if (!arg.empty() && n > 0) {
404 for (size_t i = 0; i < n; ++i)
408 handle_colalign(p2, colinfo, next);
411 cerr << "Ignoring column specification"
412 " '*{" << num << "}{"
413 << arg << "}'." << endl;
418 // text instead of the column spacing
420 // text in addition to the column spacing
421 next.special += t.character();
422 next.special += '{' + p.verbatim_item() + '}';
425 // try user defined column types
426 // unknown column types (nargs == -1) are
427 // assumed to consume no arguments
429 next.special += t.character();
431 preamble.getSpecialTableColumnArguments(t.character());
432 for (int i = 0; i < nargs; ++i)
433 next.special += '{' +
434 p.verbatim_item() + '}';
435 colinfo.push_back(next);
442 // Maybe we have some column separators that need to be added to the
445 if (!next.special.empty()) {
446 ColInfo & ci = colinfo.back();
448 ci.special += next.special;
449 next.special.erase();
455 * Move the left and right lines and alignment settings of the column \p ci
456 * to the special field if necessary.
458 void fix_colalign(ColInfo & ci)
460 if (ci.leftlines > 1 || ci.rightlines > 1)
466 * LyX can't handle more than one vertical line at the left or right side
468 * This function moves the left and right lines and alignment settings of all
469 * columns in \p colinfo to the special field if necessary.
471 void fix_colalign(vector<ColInfo> & colinfo)
473 // Try to move extra leftlines to the previous column.
474 // We do this only if both special fields are empty, otherwise we
475 // can't tell wether the result will be the same.
476 for (size_t col = 0; col < colinfo.size(); ++col) {
477 if (colinfo[col].leftlines > 1 &&
478 colinfo[col].special.empty() && col > 0 &&
479 colinfo[col - 1].rightlines == 0 &&
480 colinfo[col - 1].special.empty()) {
481 ++colinfo[col - 1].rightlines;
482 --colinfo[col].leftlines;
485 // Try to move extra rightlines to the next column
486 for (size_t col = 0; col < colinfo.size(); ++col) {
487 if (colinfo[col].rightlines > 1 &&
488 colinfo[col].special.empty() &&
489 col < colinfo.size() - 1 &&
490 colinfo[col + 1].leftlines == 0 &&
491 colinfo[col + 1].special.empty()) {
492 ++colinfo[col + 1].leftlines;
493 --colinfo[col].rightlines;
496 // Move the lines and alignment settings to the special field if
498 for (size_t col = 0; col < colinfo.size(); ++col)
499 fix_colalign(colinfo[col]);
504 * Parse hlines and similar stuff.
505 * \returns wether the token \p t was parsed
507 bool parse_hlines(Parser & p, Token const & t, string & hlines,
508 bool is_long_tabular)
510 LASSERT(t.cat() == catEscape, return false);
512 if (t.cs() == "hline" || t.cs() == "toprule" || t.cs() == "midrule" ||
513 t.cs() == "bottomrule")
514 hlines += '\\' + t.cs();
516 else if (t.cs() == "cline")
517 hlines += "\\cline{" + p.verbatim_item() + '}';
519 else if (t.cs() == "cmidrule") {
520 // We cannot handle the \cmidrule(l){3-4} form
523 bool const hasParentheses(p.getFullArg('(', ')').first);
527 hlines += "\\cmidrule{" + p.verbatim_item() + '}';
530 else if (t.cs() == "addlinespace") {
533 bool const hasArgument(p.getFullArg('{', '}').first);
536 hlines += "\\addlinespace{" + p.verbatim_item() + '}';
538 hlines += "\\addlinespace";
541 else if (is_long_tabular && t.cs() == "newpage")
542 hlines += "\\newpage";
551 /// Position in a row
553 /// At the very beginning, before the first token
555 /// After the first token and before any column token
557 /// After the first column token. Comments and whitespace are only
558 /// treated as tokens in this position
560 /// After the first non-column token at the end
566 * Parse table structure.
567 * We parse tables in a two-pass process: This function extracts the table
568 * structure (rows, columns, hlines etc.), but does not change the cell
569 * content. The cell content is parsed in a second step in handle_tabular().
571 void parse_table(Parser & p, ostream & os, bool is_long_tabular,
572 RowPosition & pos, unsigned flags)
574 // table structure commands such as \hline
577 // comments that occur at places where we can't handle them
581 Token const & t = p.get_token();
584 debugToken(cerr, t, flags);
587 // comments and whitespace in hlines
590 case IN_HLINES_START:
592 if (t.cat() == catComment) {
597 // We can't handle comments here,
598 // store them for later use
599 comments += t.asInput();
601 } else if (t.cat() == catSpace ||
602 t.cat() == catNewline) {
603 // whitespace is irrelevant here, we
604 // need to recognize hline stuff
613 // We need to handle structure stuff first in order to
614 // determine wether we need to output a HLINE separator
615 // before the row or not.
616 if (t.cat() == catEscape) {
617 if (parse_hlines(p, t, hlines, is_long_tabular)) {
620 pos = IN_HLINES_START;
625 case IN_HLINES_START:
632 else if (t.cs() == "tabularnewline" ||
636 cerr << "Warning: Converting TeX "
637 "'\\cr' to LaTeX '\\\\'."
639 // stuff before the line break
640 os << comments << HLINE << hlines << HLINE
642 //cerr << "hlines: " << hlines << endl;
649 else if (is_long_tabular &&
650 (t.cs() == "endhead" ||
651 t.cs() == "endfirsthead" ||
652 t.cs() == "endfoot" ||
653 t.cs() == "endlastfoot")) {
654 hlines += t.asInput();
658 // these commands are implicit line
660 os << comments << HLINE << hlines
667 pos = IN_HLINES_START;
669 case IN_HLINES_START:
676 // We need a HLINE separator if we either have no hline
677 // stuff at all and are just starting a row or if we just
678 // got the first non-hline token.
681 // no hline tokens exist, first token at row start
682 case IN_HLINES_START:
683 // hline tokens exist, first non-hline token at row
685 os << hlines << HLINE << comments;
691 // Oops, there is still cell content or unsupported
692 // booktabs commands after hline stuff. The latter are
693 // moved to the cell, and the first does not work in
694 // LaTeX, so we ignore the hlines.
697 if (support::contains(hlines, "\\hline") ||
698 support::contains(hlines, "\\cline") ||
699 support::contains(hlines, "\\newpage"))
700 cerr << "Ignoring '" << hlines
701 << "' in a cell" << endl;
711 // If we come here we have normal cell content
715 if (t.cat() == catMath) {
716 // we are inside some text mode thingy, so opening new math is allowed
717 Token const & n = p.get_token();
718 if (n.cat() == catMath) {
719 // TeX's $$...$$ syntax for displayed math
721 // This does only work because parse_math outputs TeX
722 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
724 p.get_token(); // skip the second '$' token
726 // simple $...$ stuff
729 // This does only work because parse_math outputs TeX
730 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
735 else if (t.cat() == catSpace
736 || t.cat() == catNewline
737 || t.cat() == catLetter
738 || t.cat() == catSuper
740 || t.cat() == catOther
741 || t.cat() == catActive
742 || t.cat() == catParameter)
745 else if (t.cat() == catBegin) {
747 parse_table(p, os, is_long_tabular, pos,
752 else if (t.cat() == catEnd) {
753 if (flags & FLAG_BRACE_LAST)
755 cerr << "unexpected '}'\n";
758 else if (t.cat() == catAlign) {
763 else if (t.cat() == catComment)
766 else if (t.cs() == "(") {
768 // This does only work because parse_math outputs TeX
769 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
773 else if (t.cs() == "[") {
775 // This does only work because parse_math outputs TeX
776 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
780 else if (t.cs() == "begin") {
781 string const name = p.getArg('{', '}');
782 active_environments.push_back(name);
783 os << "\\begin{" << name << '}';
784 // treat the nested environment as a block, don't
785 // parse &, \\ etc, because they don't belong to our
786 // table if they appear.
787 os << p.ertEnvironment(name);
788 os << "\\end{" << name << '}';
789 active_environments.pop_back();
792 else if (t.cs() == "end") {
793 if (flags & FLAG_END) {
794 // eat environment name
795 string const name = p.getArg('{', '}');
796 if (name != active_environment())
797 p.error("\\end{" + name + "} does not match \\begin{"
798 + active_environment() + "}");
801 p.error("found 'end' unexpectedly");
808 // We can have comments if the last line is incomplete
811 // We can have hline stuff if the last line is incomplete
812 if (!hlines.empty()) {
813 // this does not work in LaTeX, so we ignore it
814 cerr << "Ignoring '" << hlines << "' at end of tabular"
820 void handle_hline_above(RowInfo & ri, vector<CellInfo> & ci)
823 for (size_t col = 0; col < ci.size(); ++col)
824 ci[col].topline = true;
828 void handle_hline_below(RowInfo & ri, vector<CellInfo> & ci)
830 ri.bottomline = true;
831 for (size_t col = 0; col < ci.size(); ++col)
832 ci[col].bottomline = true;
836 } // anonymous namespace
839 void handle_tabular(Parser & p, ostream & os, string const & name,
840 string const & tabularwidth, Context & context)
842 bool const is_long_tabular(name == "longtable");
843 bool booktabs = false;
844 string tabularvalignment("middle");
845 string posopts = p.getOpt();
846 if (!posopts.empty()) {
847 // FIXME: Convert this to ERT
849 cerr << "horizontal longtable positioning '"
850 << posopts << "' ignored\n";
851 else if (posopts == "[t]")
852 tabularvalignment = "top";
853 else if (posopts == "[b]")
854 tabularvalignment = "bottom";
856 cerr << "vertical tabular positioning '"
857 << posopts << "' ignored\n";
860 vector<ColInfo> colinfo;
862 // handle column formatting
863 handle_colalign(p, colinfo, ColInfo());
864 fix_colalign(colinfo);
866 // first scan of cells
867 // use table mode to keep it minimal-invasive
868 // not exactly what's TeX doing...
869 vector<string> lines;
871 RowPosition rowpos = ROW_START;
872 parse_table(p, ss, is_long_tabular, rowpos, FLAG_END);
873 split(ss.str(), lines, LINE);
875 vector< vector<CellInfo> > cellinfo(lines.size());
876 vector<RowInfo> rowinfo(lines.size());
883 //cerr << "// split into rows\n";
884 for (size_t row = 0; row < rowinfo.size();) {
887 cellinfo[row].resize(colinfo.size());
888 bool deletelastrow = false;
891 vector<string> dummy;
892 //cerr << "\n########### LINE: " << lines[row] << "########\n";
893 split(lines[row], dummy, HLINE);
895 // handle horizontal line fragments
896 // we do only expect this for a last line without '\\'
897 if (dummy.size() != 3) {
898 if ((dummy.size() != 1 && dummy.size() != 2) ||
899 row != rowinfo.size() - 1)
900 cerr << "unexpected dummy size: " << dummy.size()
901 << " content: " << lines[row] << "\n";
904 lines[row] = dummy[1];
906 //cerr << "line: " << row << " above 0: " << dummy[0] << "\n";
907 //cerr << "line: " << row << " below 2: " << dummy[2] << "\n";
908 //cerr << "line: " << row << " cells 1: " << dummy[1] << "\n";
910 for (int i = 0; i <= 2; i += 2) {
911 //cerr << " reading from line string '" << dummy[i] << "'\n";
914 Token t = p1.get_token();
915 //cerr << "read token: " << t << "\n";
916 if (t.cs() == "hline" || t.cs() == "toprule" ||
917 t.cs() == "midrule" ||
918 t.cs() == "bottomrule") {
919 if (t.cs() != "hline")
922 if (rowinfo[row].topline) {
923 if (row > 0) // extra bottomline above
924 handle_hline_below(rowinfo[row - 1], cellinfo[row - 1]);
926 cerr << "dropping extra "
928 //cerr << "below row: " << row-1 << endl;
930 handle_hline_above(rowinfo[row], cellinfo[row]);
931 //cerr << "above row: " << row << endl;
934 //cerr << "below row: " << row << endl;
935 handle_hline_below(rowinfo[row], cellinfo[row]);
937 } else if (t.cs() == "cline" || t.cs() == "cmidrule") {
938 if (t.cs() == "cmidrule")
940 string arg = p1.verbatim_item();
941 //cerr << "read " << t.cs() << " arg: '" << arg << "'\n";
943 split(arg, cols, '-');
945 size_t from = convert<unsigned int>(cols[0]);
947 cerr << "Could not parse "
948 << t.cs() << " start column."
951 // 1 based index -> 0 based
953 if (from >= colinfo.size()) {
954 cerr << t.cs() << " starts at "
955 "non existing column "
956 << (from + 1) << endl;
957 from = colinfo.size() - 1;
959 size_t to = convert<unsigned int>(cols[1]);
961 cerr << "Could not parse "
962 << t.cs() << " end column."
965 // 1 based index -> 0 based
967 if (to >= colinfo.size()) {
968 cerr << t.cs() << " ends at "
969 "non existing column "
971 to = colinfo.size() - 1;
973 for (size_t col = from; col <= to; ++col) {
974 //cerr << "row: " << row << " col: " << col << " i: " << i << endl;
976 rowinfo[row].topline = true;
977 cellinfo[row][col].topline = true;
979 rowinfo[row].bottomline = true;
980 cellinfo[row][col].bottomline = true;
983 } else if (t.cs() == "addlinespace") {
985 string const opt = p.next_token().cat() == catBegin ?
986 p.verbatim_item() : string();
989 rowinfo[row].top_space = "default";
991 rowinfo[row].top_space = translate_len(opt);
992 } else if (rowinfo[row].bottomline) {
994 rowinfo[row].bottom_space = "default";
996 rowinfo[row].bottom_space = translate_len(opt);
999 rowinfo[row].interline_space = "default";
1001 rowinfo[row].interline_space = translate_len(opt);
1003 } else if (t.cs() == "endhead") {
1005 endhead.empty = true;
1007 rowinfo[row].type = LT_HEAD;
1008 for (int r = row - 1; r >= 0; --r) {
1009 if (rowinfo[r].type != LT_NORMAL)
1011 rowinfo[r].type = LT_HEAD;
1012 endhead.empty = false;
1015 } else if (t.cs() == "endfirsthead") {
1017 endfirsthead.empty = true;
1019 rowinfo[row].type = LT_FIRSTHEAD;
1020 for (int r = row - 1; r >= 0; --r) {
1021 if (rowinfo[r].type != LT_NORMAL)
1023 rowinfo[r].type = LT_FIRSTHEAD;
1024 endfirsthead.empty = false;
1026 endfirsthead.set = true;
1027 } else if (t.cs() == "endfoot") {
1029 endfoot.empty = true;
1031 rowinfo[row].type = LT_FOOT;
1032 for (int r = row - 1; r >= 0; --r) {
1033 if (rowinfo[r].type != LT_NORMAL)
1035 rowinfo[r].type = LT_FOOT;
1036 endfoot.empty = false;
1039 } else if (t.cs() == "endlastfoot") {
1041 endlastfoot.empty = true;
1043 rowinfo[row].type = LT_LASTFOOT;
1044 for (int r = row - 1; r >= 0; --r) {
1045 if (rowinfo[r].type != LT_NORMAL)
1047 rowinfo[r].type = LT_LASTFOOT;
1048 endlastfoot.empty = false;
1050 endlastfoot.set = true;
1051 } else if (t.cs() == "newpage") {
1054 rowinfo[row - 1].newpage = true;
1056 // This does not work in LaTeX
1062 rowinfo[row].newpage = true;
1064 cerr << "unexpected line token: " << t << endl;
1069 // LyX ends headers and footers always with \tabularnewline.
1070 // This causes one additional row in the output.
1071 // If the last row of a header/footer is empty, we can work
1072 // around that by removing it.
1074 RowInfo test = rowinfo[row-1];
1075 test.type = LT_NORMAL;
1076 if (lines[row-1].empty() && !test.special()) {
1077 switch (rowinfo[row-1].type) {
1079 if (rowinfo[row].type != LT_FIRSTHEAD &&
1080 rowinfo[row-2].type == LT_FIRSTHEAD)
1081 deletelastrow = true;
1084 if (rowinfo[row].type != LT_HEAD &&
1085 rowinfo[row-2].type == LT_HEAD)
1086 deletelastrow = true;
1089 if (rowinfo[row].type != LT_FOOT &&
1090 rowinfo[row-2].type == LT_FOOT)
1091 deletelastrow = true;
1094 if (rowinfo[row].type != LT_LASTFOOT &&
1095 rowinfo[row-2].type == LT_LASTFOOT)
1096 deletelastrow = true;
1104 if (deletelastrow) {
1105 lines.erase(lines.begin() + (row - 1));
1106 rowinfo.erase(rowinfo.begin() + (row - 1));
1107 cellinfo.erase(cellinfo.begin() + (row - 1));
1112 vector<string> cells;
1113 split(lines[row], cells, TAB);
1114 for (size_t col = 0, cell = 0; cell < cells.size();
1116 //cerr << "cell content: '" << cells[cell] << "'\n";
1117 if (col >= colinfo.size()) {
1118 // This does not work in LaTeX
1119 cerr << "Ignoring extra cell '"
1120 << cells[cell] << "'." << endl;
1123 Parser p(cells[cell]);
1125 //cells[cell] << "'\n";
1126 if (p.next_token().cs() == "multicolumn") {
1129 size_t const ncells =
1130 convert<unsigned int>(p.verbatim_item());
1132 // special cell properties alignment
1134 handle_colalign(p, t, ColInfo());
1135 p.skip_spaces(true);
1136 ColInfo & ci = t.front();
1138 // The logic of LyX for multicolumn vertical
1139 // lines is too complicated to reproduce it
1140 // here (see LyXTabular::TeXCellPreamble()).
1141 // Therefore we simply put everything in the
1145 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1146 cellinfo[row][col].align = ci.align;
1147 cellinfo[row][col].special = ci.special;
1148 cellinfo[row][col].leftlines = ci.leftlines;
1149 cellinfo[row][col].rightlines = ci.rightlines;
1151 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
1152 if (!cellinfo[row][col].content.empty()) {
1153 // This may or may not work in LaTeX,
1154 // but it does not work in LyX.
1155 // FIXME: Handle it correctly!
1156 cerr << "Moving cell content '"
1158 << "' into a multicolumn cell. "
1159 "This will probably not work."
1162 cellinfo[row][col].content += os.str();
1164 // add dummy cells for multicol
1165 for (size_t i = 0; i < ncells - 1; ++i) {
1167 if (col >= colinfo.size()) {
1168 cerr << "The cell '"
1171 << convert<string>(ncells)
1172 << " columns while the table has only "
1173 << convert<string>(colinfo.size())
1175 << " Therefore the surplus columns will be ignored."
1179 cellinfo[row][col].multi = CELL_PART_OF_MULTICOLUMN;
1180 cellinfo[row][col].align = 'c';
1183 } else if (col == 0 && colinfo.size() > 1 &&
1185 p.next_token().cs() == "caption") {
1186 // longtable caption support in LyX is a hack:
1187 // Captions require a row of their own with
1188 // the caption flag set to true, having only
1189 // one multicolumn cell. The contents of that
1190 // cell must contain exactly one caption inset
1191 // and nothing else.
1192 // Fortunately, the caption flag is only needed
1193 // for tables with more than one column.
1194 rowinfo[row].caption = true;
1195 for (size_t c = 1; c < cells.size(); ++c) {
1196 if (!cells[c].empty()) {
1197 cerr << "Moving cell content '"
1199 << "' into the caption cell. "
1200 "This will probably not work."
1202 cells[0] += cells[c];
1206 cellinfo[row][col].align = colinfo[col].align;
1207 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1209 parse_text_in_inset(p, os, FLAG_CELL, false, context);
1210 cellinfo[row][col].content += os.str();
1211 // add dummy multicolumn cells
1212 for (size_t c = 1; c < colinfo.size(); ++c)
1213 cellinfo[row][c].multi = CELL_PART_OF_MULTICOLUMN;
1217 if (p.next_token().cs() == "begin") {
1220 string const env = p.getArg('{', '}');
1221 if (env == "sideways" || env == "turn") {
1222 string angle = "90";
1223 if (env == "turn") {
1225 angle = p.getArg('{', '}');
1227 active_environments.push_back(env);
1228 p.ertEnvironment(env);
1229 active_environments.pop_back();
1231 if (!p.good() && support::isStrInt(angle))
1232 rotate = convert<int>(angle);
1236 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1237 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1238 cellinfo[row][col].align = colinfo[col].align;
1241 cellinfo[row][col].rotate = rotate;
1243 active_environments.push_back(p.getArg('{', '}'));
1246 parse_text_in_inset(p, os, FLAG_END, false, context);
1247 active_environments.pop_back();
1248 preamble.registerAutomaticallyLoadedPackage("rotating");
1250 parse_text_in_inset(p, os, FLAG_CELL, false, context);
1252 cellinfo[row][col].content += os.str();
1256 //cerr << "// handle almost empty last row what we have\n";
1257 // handle almost empty last row
1258 if (row && lines[row].empty() && row + 1 == rowinfo.size()) {
1259 //cerr << "remove empty last line\n";
1260 if (rowinfo[row].topline)
1261 rowinfo[row - 1].bottomline = true;
1262 for (size_t col = 0; col < colinfo.size(); ++col)
1263 if (cellinfo[row][col].topline)
1264 cellinfo[row - 1][col].bottomline = true;
1271 // Now we have the table structure and content in rowinfo, colinfo
1273 // Unfortunately LyX has some limitations that we need to work around.
1275 // Convert cells with special content to multicolumn cells
1276 // (LyX ignores the special field for non-multicolumn cells).
1277 for (size_t row = 0; row < rowinfo.size(); ++row) {
1278 for (size_t col = 0; col < cellinfo[row].size(); ++col) {
1279 if (cellinfo[row][col].multi == CELL_NORMAL &&
1280 !cellinfo[row][col].special.empty())
1281 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1285 // Distribute lines from rows/columns to cells
1286 // The code was stolen from convert_tablines() in lyx2lyx/lyx_1_6.py.
1287 // Each standard cell inherits the settings of the corresponding
1288 // rowinfo/colinfo. This works because all cells with individual
1289 // settings were converted to multicolumn cells above.
1290 // Each multicolumn cell inherits the settings of the rowinfo/colinfo
1291 // corresponding to the first column of the multicolumn cell (default
1292 // of the multicol package). This works because the special field
1293 // overrides the line fields.
1294 for (size_t row = 0; row < rowinfo.size(); ++row) {
1295 for (size_t col = 0; col < cellinfo[row].size(); ++col) {
1296 if (cellinfo[row][col].multi == CELL_NORMAL) {
1297 cellinfo[row][col].topline = rowinfo[row].topline;
1298 cellinfo[row][col].bottomline = rowinfo[row].bottomline;
1299 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1300 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1301 } else if (cellinfo[row][col].multi == CELL_BEGIN_OF_MULTICOLUMN) {
1303 while (s < cellinfo[row].size() &&
1304 cellinfo[row][s].multi == CELL_PART_OF_MULTICOLUMN)
1306 if (s < cellinfo[row].size() &&
1307 cellinfo[row][s].multi != CELL_BEGIN_OF_MULTICOLUMN)
1308 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1309 if (col > 0 && cellinfo[row][col-1].multi == CELL_NORMAL)
1310 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1316 preamble.registerAutomaticallyLoadedPackage("booktabs");
1317 if (is_long_tabular)
1318 preamble.registerAutomaticallyLoadedPackage("longtable");
1320 //cerr << "// output what we have\n";
1321 // output what we have
1322 string const rotate = "0";
1323 os << "\n<lyxtabular version=\"3\" rows=\"" << rowinfo.size()
1324 << "\" columns=\"" << colinfo.size() << "\">\n";
1326 << write_attribute("rotate", rotate)
1327 << write_attribute("booktabs", booktabs)
1328 << write_attribute("islongtable", is_long_tabular);
1329 if (is_long_tabular) {
1330 os << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1331 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1332 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1333 << write_attribute("headTopDL", endhead.topDL)
1334 << write_attribute("headBottomDL", endhead.bottomDL)
1335 << write_attribute("footTopDL", endfoot.topDL)
1336 << write_attribute("footBottomDL", endfoot.bottomDL)
1337 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1338 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1339 << write_attribute("lastFootEmpty", endlastfoot.empty);
1341 os << write_attribute("tabularvalignment", tabularvalignment)
1342 << write_attribute("tabularwidth", tabularwidth);
1345 //cerr << "// after header\n";
1346 for (size_t col = 0; col < colinfo.size(); ++col) {
1347 os << "<column alignment=\""
1348 << verbose_align(colinfo[col].align) << "\""
1350 << verbose_valign(colinfo[col].valign) << "\""
1351 << write_attribute("width", translate_len(colinfo[col].width))
1352 << write_attribute("special", colinfo[col].special)
1355 //cerr << "// after cols\n";
1357 for (size_t row = 0; row < rowinfo.size(); ++row) {
1359 << write_attribute("topspace", rowinfo[row].top_space)
1360 << write_attribute("bottomspace", rowinfo[row].bottom_space)
1361 << write_attribute("interlinespace", rowinfo[row].interline_space)
1362 << write_attribute("endhead",
1363 rowinfo[row].type == LT_HEAD)
1364 << write_attribute("endfirsthead",
1365 rowinfo[row].type == LT_FIRSTHEAD)
1366 << write_attribute("endfoot",
1367 rowinfo[row].type == LT_FOOT)
1368 << write_attribute("endlastfoot",
1369 rowinfo[row].type == LT_LASTFOOT)
1370 << write_attribute("newpage", rowinfo[row].newpage)
1371 << write_attribute("caption", rowinfo[row].caption)
1373 for (size_t col = 0; col < colinfo.size(); ++col) {
1374 CellInfo const & cell = cellinfo[row][col];
1376 if (cell.multi != CELL_NORMAL)
1377 os << " multicolumn=\"" << cell.multi << "\"";
1378 os << " alignment=\"" << verbose_align(cell.align)
1380 << " valignment=\"" << verbose_valign(cell.valign)
1382 << write_attribute("topline", cell.topline)
1383 << write_attribute("bottomline", cell.bottomline)
1384 << write_attribute("leftline", cell.leftlines > 0)
1385 << write_attribute("rightline", cell.rightlines > 0)
1386 << write_attribute("rotate", cell.rotate);
1387 //cerr << "\nrow: " << row << " col: " << col;
1389 // cerr << " topline=\"true\"";
1390 //if (cell.bottomline)
1391 // cerr << " bottomline=\"true\"";
1392 os << " usebox=\"none\""
1393 << write_attribute("width", translate_len(cell.width));
1394 if (cell.multi != CELL_NORMAL)
1395 os << write_attribute("special", cell.special);
1397 << "\n\\begin_inset Text\n"
1399 << "\n\\end_inset\n"
1405 os << "</lyxtabular>\n";