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"
34 // filled in preamble.cpp
35 map<char, int> special_columns;
42 ColInfo() : align('n'), valign('n'), rightlines(0), leftlines(0) {}
45 /// vertical alignment
49 /// special column alignment
51 /// number of lines on the right
53 /// number of lines on the left
58 /// row type for longtables
65 /// part of head on first page
69 /// part of foot on last page
76 RowInfo() : topline(false), bottomline(false), type(LT_NORMAL),
77 caption(false), newpage(false) {}
78 /// Does this row have any special setting?
81 return topline || bottomline || !top_space.empty() ||
82 !bottom_space.empty() || !interline_space.empty() ||
83 type != LT_NORMAL || caption || newpage;
85 /// horizontal line above
87 /// horizontal line below
89 /// Extra space between the top line and this row
91 /// Extra space between this row and the bottom line
93 /// Extra space between the bottom line and the next top line
94 string interline_space;
95 /// These are for longtabulars only
96 /// row type (head, foot, firsthead etc.)
100 /// row for a newpage
105 /// the numeric values are part of the file format!
109 /// A multicolumn cell. The number of columns is <tt>1 + number
110 /// of CELL_PART_OF_MULTICOLUMN cells</tt> that follow directly
111 CELL_BEGIN_OF_MULTICOLUMN = 1,
112 /// This is a dummy cell (part of a multicolumn cell)
113 CELL_PART_OF_MULTICOLUMN = 2
119 CellInfo() : multi(CELL_NORMAL), align('n'), valign('n'),
120 leftlines(0), rightlines(0), topline(false),
121 bottomline(false), rotate(0) {}
128 /// vertical cell alignment
130 /// number of lines on the left
132 /// number of lines on the right
134 /// do we have a line above?
136 /// do we have a line below?
138 /// how is the cell rotated?
140 /// width for multicolumn cells
142 /// special formatting for multicolumn cells
150 ltType() : topDL(false), bottomDL(false), empty(false) {}
151 // we have this header type (is set in the getLT... functions)
153 // double borders on top
155 // double borders on bottom
157 // used for FirstHeader & LastFooter and if this is true
158 // all the rows marked as FirstHeader or LastFooter are
159 // ignored in the output and it is set to be empty!
164 /// translate a horizontal alignment (as stored in ColInfo and CellInfo) to LyX
165 inline char const * verbose_align(char c)
180 /// translate a vertical alignment (as stored in ColInfo and CellInfo) to LyX
181 inline char const * verbose_valign(char c)
183 // The default value for no special alignment is "top".
196 // stripped down from tabluar.C. We use it currently only for bools and
198 string const write_attribute(string const & name, bool const & b)
200 // we write only true attribute values so we remove a bit of the
201 // file format bloat for tabulars.
202 return b ? ' ' + name + "=\"true\"" : string();
206 string const write_attribute(string const & name, string const & s)
208 return s.empty() ? string() : ' ' + name + "=\"" + s + '"';
212 string const write_attribute(string const & name, int const & i)
214 // we write only true attribute values so we remove a bit of the
215 // file format bloat for tabulars.
216 return i ? write_attribute(name, convert<string>(i)) : string();
220 /*! rather brutish way to code table structure in a string:
225 \multicolumn{2}{c}{4} & 5 //
231 gets "translated" to:
234 HLINE 1 TAB 2 TAB 3 HLINE HLINE LINE
235 \hline HLINE \multicolumn{2}{c}{4} TAB 5 HLINE HLINE LINE
236 HLINE 6 TAB 7 HLINE HLINE LINE
237 HLINE 8 HLINE \endhead HLINE LINE
241 char const TAB = '\001';
242 char const LINE = '\002';
243 char const HLINE = '\004';
247 * Move the information in leftlines, rightlines, align and valign to the
248 * special field. This is necessary if the special field is not empty,
249 * because LyX ignores leftlines > 1, rightlines > 1, align and valign in
252 void ci2special(ColInfo & ci)
254 if (ci.width.empty() && ci.align == 'n')
255 // The alignment setting is already in special, since
256 // handle_colalign() never stores ci with these settings
257 // and ensures that leftlines == 0 and rightlines == 0 in
261 if (!ci.width.empty()) {
264 ci.special += ">{\\raggedright}";
267 ci.special += ">{\\raggedleft}";
270 ci.special += ">{\\centering}";
273 if (ci.valign == 'n')
276 ci.special += ci.valign;
277 ci.special += '{' + ci.width + '}';
280 ci.special += ci.align;
282 // LyX can only have one left and one right line.
283 for (int i = 1; i < ci.leftlines; ++i)
284 ci.special.insert(0, "|");
285 for (int i = 1; i < ci.rightlines; ++i)
287 ci.leftlines = min(ci.leftlines, 1);
288 ci.rightlines = min(ci.rightlines, 1);
295 * Handle column specifications for tabulars and multicolumns.
296 * The next token of the parser \p p must be an opening brace, and we read
297 * everything until the matching closing brace.
298 * The resulting column specifications are filled into \p colinfo. This is
299 * in an intermediate form. fix_colalign() makes it suitable for LyX output.
301 void handle_colalign(Parser & p, vector<ColInfo> & colinfo,
302 ColInfo const & start)
304 if (p.get_token().cat() != catBegin)
305 cerr << "Wrong syntax for table column alignment.\n"
306 "Expected '{', got '" << p.curr_token().asInput()
309 ColInfo next = start;
310 for (Token t = p.get_token(); p.good() && t.cat() != catEnd;
313 cerr << "t: " << t << " c: '" << t.character() << "'\n";
316 // We cannot handle comments here
317 if (t.cat() == catComment) {
318 if (t.cs().empty()) {
322 cerr << "Ignoring comment: " << t.asInput();
326 switch (t.character()) {
330 // new column, horizontal aligned
331 next.align = t.character();
332 if (!next.special.empty())
334 colinfo.push_back(next);
340 // new column, vertical aligned box
341 next.valign = t.character();
342 next.width = p.verbatim_item();
343 if (!next.special.empty())
345 colinfo.push_back(next);
350 if (colinfo.empty()) {
351 if (next.special.empty())
355 } else if (colinfo.back().special.empty())
356 ++colinfo.back().rightlines;
357 else if (next.special.empty())
360 colinfo.back().special += '|';
363 // text before the next column
364 string const s = trimSpaceAndEol(p.verbatim_item());
365 if (next.special.empty() &&
367 // Maybe this can be converted to a
368 // horizontal alignment setting for
369 // fixed width columns
370 if (s == "\\raggedleft")
372 else if (s == "\\raggedright")
374 else if (s == "\\centering")
377 next.special = ">{" + s + '}';
379 next.special += ">{" + s + '}';
383 // text after the last column
384 string const s = trimSpaceAndEol(p.verbatim_item());
386 // This is not possible in LaTeX.
387 cerr << "Ignoring separator '<{"
388 << s << "}'." << endl;
390 ColInfo & ci = colinfo.back();
392 ci.special += "<{" + s + '}';
397 // *{n}{arg} means 'n' columns of type 'arg'
398 string const num = p.verbatim_item();
399 string const arg = p.verbatim_item();
400 size_t const n = convert<unsigned int>(num);
401 if (!arg.empty() && n > 0) {
403 for (size_t i = 0; i < n; ++i)
407 handle_colalign(p2, colinfo, next);
410 cerr << "Ignoring column specification"
411 " '*{" << num << "}{"
412 << arg << "}'." << endl;
417 // text instead of the column spacing
419 // text in addition to the column spacing
420 next.special += t.character();
421 next.special += '{' + p.verbatim_item() + '}';
424 // try user defined column types
425 if (special_columns.find(t.character()) !=
426 special_columns.end()) {
428 next.special += t.character();
430 special_columns[t.character()];
431 for (int i = 0; i < nargs; ++i)
432 next.special += '{' +
435 colinfo.push_back(next);
438 // unknown column specifier, assume no arguments
440 next.special += t.character();
441 colinfo.push_back(next);
448 // Maybe we have some column separators that need to be added to the
451 if (!next.special.empty()) {
452 ColInfo & ci = colinfo.back();
454 ci.special += next.special;
455 next.special.erase();
461 * Move the left and right lines and alignment settings of the column \p ci
462 * to the special field if necessary.
464 void fix_colalign(ColInfo & ci)
466 if (ci.leftlines > 1 || ci.rightlines > 1)
472 * LyX can't handle more than one vertical line at the left or right side
474 * This function moves the left and right lines and alignment settings of all
475 * columns in \p colinfo to the special field if necessary.
477 void fix_colalign(vector<ColInfo> & colinfo)
479 // Try to move extra leftlines to the previous column.
480 // We do this only if both special fields are empty, otherwise we
481 // can't tell wether the result will be the same.
482 for (size_t col = 0; col < colinfo.size(); ++col) {
483 if (colinfo[col].leftlines > 1 &&
484 colinfo[col].special.empty() && col > 0 &&
485 colinfo[col - 1].rightlines == 0 &&
486 colinfo[col - 1].special.empty()) {
487 ++colinfo[col - 1].rightlines;
488 --colinfo[col].leftlines;
491 // Try to move extra rightlines to the next column
492 for (size_t col = 0; col < colinfo.size(); ++col) {
493 if (colinfo[col].rightlines > 1 &&
494 colinfo[col].special.empty() &&
495 col < colinfo.size() - 1 &&
496 colinfo[col + 1].leftlines == 0 &&
497 colinfo[col + 1].special.empty()) {
498 ++colinfo[col + 1].leftlines;
499 --colinfo[col].rightlines;
502 // Move the lines and alignment settings to the special field if
504 for (size_t col = 0; col < colinfo.size(); ++col)
505 fix_colalign(colinfo[col]);
510 * Parse hlines and similar stuff.
511 * \returns wether the token \p t was parsed
513 bool parse_hlines(Parser & p, Token const & t, string & hlines,
514 bool is_long_tabular)
516 LASSERT(t.cat() == catEscape, return false);
518 if (t.cs() == "hline" || t.cs() == "toprule" || t.cs() == "midrule" ||
519 t.cs() == "bottomrule")
520 hlines += '\\' + t.cs();
522 else if (t.cs() == "cline")
523 hlines += "\\cline{" + p.verbatim_item() + '}';
525 else if (t.cs() == "cmidrule") {
526 // We cannot handle the \cmidrule(l){3-4} form
529 bool const hasParentheses(p.getFullArg('(', ')').first);
533 hlines += "\\cmidrule{" + p.verbatim_item() + '}';
536 else if (t.cs() == "addlinespace") {
539 bool const hasArgument(p.getFullArg('{', '}').first);
542 hlines += "\\addlinespace{" + p.verbatim_item() + '}';
544 hlines += "\\addlinespace";
547 else if (is_long_tabular && t.cs() == "newpage")
548 hlines += "\\newpage";
557 /// Position in a row
559 /// At the very beginning, before the first token
561 /// After the first token and before any column token
563 /// After the first column token. Comments and whitespace are only
564 /// treated as tokens in this position
566 /// After the first non-column token at the end
572 * Parse table structure.
573 * We parse tables in a two-pass process: This function extracts the table
574 * structure (rows, columns, hlines etc.), but does not change the cell
575 * content. The cell content is parsed in a second step in handle_tabular().
577 void parse_table(Parser & p, ostream & os, bool is_long_tabular,
578 RowPosition & pos, unsigned flags)
580 // table structure commands such as \hline
583 // comments that occur at places where we can't handle them
587 Token const & t = p.get_token();
590 debugToken(cerr, t, flags);
593 // comments and whitespace in hlines
596 case IN_HLINES_START:
598 if (t.cat() == catComment) {
603 // We can't handle comments here,
604 // store them for later use
605 comments += t.asInput();
607 } else if (t.cat() == catSpace ||
608 t.cat() == catNewline) {
609 // whitespace is irrelevant here, we
610 // need to recognize hline stuff
619 // We need to handle structure stuff first in order to
620 // determine wether we need to output a HLINE separator
621 // before the row or not.
622 if (t.cat() == catEscape) {
623 if (parse_hlines(p, t, hlines, is_long_tabular)) {
626 pos = IN_HLINES_START;
631 case IN_HLINES_START:
638 else if (t.cs() == "tabularnewline" ||
642 cerr << "Warning: Converting TeX "
643 "'\\cr' to LaTeX '\\\\'."
645 // stuff before the line break
646 os << comments << HLINE << hlines << HLINE
648 //cerr << "hlines: " << hlines << endl;
655 else if (is_long_tabular &&
656 (t.cs() == "endhead" ||
657 t.cs() == "endfirsthead" ||
658 t.cs() == "endfoot" ||
659 t.cs() == "endlastfoot")) {
660 hlines += t.asInput();
664 // these commands are implicit line
666 os << comments << HLINE << hlines
673 pos = IN_HLINES_START;
675 case IN_HLINES_START:
682 // We need a HLINE separator if we either have no hline
683 // stuff at all and are just starting a row or if we just
684 // got the first non-hline token.
687 // no hline tokens exist, first token at row start
688 case IN_HLINES_START:
689 // hline tokens exist, first non-hline token at row
691 os << hlines << HLINE << comments;
697 // Oops, there is still cell content or unsupported
698 // booktabs commands after hline stuff. The latter are
699 // moved to the cell, and the first does not work in
700 // LaTeX, so we ignore the hlines.
703 if (support::contains(hlines, "\\hline") ||
704 support::contains(hlines, "\\cline") ||
705 support::contains(hlines, "\\newpage"))
706 cerr << "Ignoring '" << hlines
707 << "' in a cell" << endl;
717 // If we come here we have normal cell content
721 if (t.cat() == catMath) {
722 // we are inside some text mode thingy, so opening new math is allowed
723 Token const & n = p.get_token();
724 if (n.cat() == catMath) {
725 // TeX's $$...$$ syntax for displayed math
727 // This does only work because parse_math outputs TeX
728 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
730 p.get_token(); // skip the second '$' token
732 // simple $...$ stuff
735 // This does only work because parse_math outputs TeX
736 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
741 else if (t.cat() == catSpace
742 || t.cat() == catNewline
743 || t.cat() == catLetter
744 || t.cat() == catSuper
746 || t.cat() == catOther
747 || t.cat() == catActive
748 || t.cat() == catParameter)
751 else if (t.cat() == catBegin) {
753 parse_table(p, os, is_long_tabular, pos,
758 else if (t.cat() == catEnd) {
759 if (flags & FLAG_BRACE_LAST)
761 cerr << "unexpected '}'\n";
764 else if (t.cat() == catAlign) {
769 else if (t.cat() == catComment)
772 else if (t.cs() == "(") {
774 // This does only work because parse_math outputs TeX
775 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
779 else if (t.cs() == "[") {
781 // This does only work because parse_math outputs TeX
782 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
786 else if (t.cs() == "begin") {
787 string const name = p.getArg('{', '}');
788 active_environments.push_back(name);
789 os << "\\begin{" << name << '}';
790 // treat the nested environment as a block, don't
791 // parse &, \\ etc, because they don't belong to our
792 // table if they appear.
793 os << p.ertEnvironment(name);
794 os << "\\end{" << name << '}';
795 active_environments.pop_back();
798 else if (t.cs() == "end") {
799 if (flags & FLAG_END) {
800 // eat environment name
801 string const name = p.getArg('{', '}');
802 if (name != active_environment())
803 p.error("\\end{" + name + "} does not match \\begin{"
804 + active_environment() + "}");
807 p.error("found 'end' unexpectedly");
814 // We can have comments if the last line is incomplete
817 // We can have hline stuff if the last line is incomplete
818 if (!hlines.empty()) {
819 // this does not work in LaTeX, so we ignore it
820 cerr << "Ignoring '" << hlines << "' at end of tabular"
826 void handle_hline_above(RowInfo & ri, vector<CellInfo> & ci)
829 for (size_t col = 0; col < ci.size(); ++col)
830 ci[col].topline = true;
834 void handle_hline_below(RowInfo & ri, vector<CellInfo> & ci)
836 ri.bottomline = true;
837 for (size_t col = 0; col < ci.size(); ++col)
838 ci[col].bottomline = true;
842 } // anonymous namespace
845 void handle_tabular(Parser & p, ostream & os, string const & name,
846 string const & tabularwidth, Context & context)
848 bool const is_long_tabular(name == "longtable");
849 bool booktabs = false;
850 string tabularvalignment("middle");
851 string posopts = p.getOpt();
852 if (!posopts.empty()) {
853 // FIXME: Convert this to ERT
855 cerr << "horizontal longtable positioning '"
856 << posopts << "' ignored\n";
857 else if (posopts == "[t]")
858 tabularvalignment = "top";
859 else if (posopts == "[b]")
860 tabularvalignment = "bottom";
862 cerr << "vertical tabular positioning '"
863 << posopts << "' ignored\n";
866 vector<ColInfo> colinfo;
868 // handle column formatting
869 handle_colalign(p, colinfo, ColInfo());
870 fix_colalign(colinfo);
872 // first scan of cells
873 // use table mode to keep it minimal-invasive
874 // not exactly what's TeX doing...
875 vector<string> lines;
877 RowPosition rowpos = ROW_START;
878 parse_table(p, ss, is_long_tabular, rowpos, FLAG_END);
879 split(ss.str(), lines, LINE);
881 vector< vector<CellInfo> > cellinfo(lines.size());
882 vector<RowInfo> rowinfo(lines.size());
889 //cerr << "// split into rows\n";
890 for (size_t row = 0; row < rowinfo.size();) {
893 cellinfo[row].resize(colinfo.size());
894 bool deletelastrow = false;
897 vector<string> dummy;
898 //cerr << "\n########### LINE: " << lines[row] << "########\n";
899 split(lines[row], dummy, HLINE);
901 // handle horizontal line fragments
902 // we do only expect this for a last line without '\\'
903 if (dummy.size() != 3) {
904 if ((dummy.size() != 1 && dummy.size() != 2) ||
905 row != rowinfo.size() - 1)
906 cerr << "unexpected dummy size: " << dummy.size()
907 << " content: " << lines[row] << "\n";
910 lines[row] = dummy[1];
912 //cerr << "line: " << row << " above 0: " << dummy[0] << "\n";
913 //cerr << "line: " << row << " below 2: " << dummy[2] << "\n";
914 //cerr << "line: " << row << " cells 1: " << dummy[1] << "\n";
916 for (int i = 0; i <= 2; i += 2) {
917 //cerr << " reading from line string '" << dummy[i] << "'\n";
920 Token t = p1.get_token();
921 //cerr << "read token: " << t << "\n";
922 if (t.cs() == "hline" || t.cs() == "toprule" ||
923 t.cs() == "midrule" ||
924 t.cs() == "bottomrule") {
925 if (t.cs() != "hline")
928 if (rowinfo[row].topline) {
929 if (row > 0) // extra bottomline above
930 handle_hline_below(rowinfo[row - 1], cellinfo[row - 1]);
932 cerr << "dropping extra "
934 //cerr << "below row: " << row-1 << endl;
936 handle_hline_above(rowinfo[row], cellinfo[row]);
937 //cerr << "above row: " << row << endl;
940 //cerr << "below row: " << row << endl;
941 handle_hline_below(rowinfo[row], cellinfo[row]);
943 } else if (t.cs() == "cline" || t.cs() == "cmidrule") {
944 if (t.cs() == "cmidrule")
946 string arg = p1.verbatim_item();
947 //cerr << "read " << t.cs() << " arg: '" << arg << "'\n";
949 split(arg, cols, '-');
951 size_t from = convert<unsigned int>(cols[0]);
953 cerr << "Could not parse "
954 << t.cs() << " start column."
957 // 1 based index -> 0 based
959 if (from >= colinfo.size()) {
960 cerr << t.cs() << " starts at "
961 "non existing column "
962 << (from + 1) << endl;
963 from = colinfo.size() - 1;
965 size_t to = convert<unsigned int>(cols[1]);
967 cerr << "Could not parse "
968 << t.cs() << " end column."
971 // 1 based index -> 0 based
973 if (to >= colinfo.size()) {
974 cerr << t.cs() << " ends at "
975 "non existing column "
977 to = colinfo.size() - 1;
979 for (size_t col = from; col <= to; ++col) {
980 //cerr << "row: " << row << " col: " << col << " i: " << i << endl;
982 rowinfo[row].topline = true;
983 cellinfo[row][col].topline = true;
985 rowinfo[row].bottomline = true;
986 cellinfo[row][col].bottomline = true;
989 } else if (t.cs() == "addlinespace") {
991 string const opt = p.next_token().cat() == catBegin ?
992 p.verbatim_item() : string();
995 rowinfo[row].top_space = "default";
997 rowinfo[row].top_space = translate_len(opt);
998 } else if (rowinfo[row].bottomline) {
1000 rowinfo[row].bottom_space = "default";
1002 rowinfo[row].bottom_space = translate_len(opt);
1005 rowinfo[row].interline_space = "default";
1007 rowinfo[row].interline_space = translate_len(opt);
1009 } else if (t.cs() == "endhead") {
1011 endhead.empty = true;
1013 rowinfo[row].type = LT_HEAD;
1014 for (int r = row - 1; r >= 0; --r) {
1015 if (rowinfo[r].type != LT_NORMAL)
1017 rowinfo[r].type = LT_HEAD;
1018 endhead.empty = false;
1021 } else if (t.cs() == "endfirsthead") {
1023 endfirsthead.empty = true;
1025 rowinfo[row].type = LT_FIRSTHEAD;
1026 for (int r = row - 1; r >= 0; --r) {
1027 if (rowinfo[r].type != LT_NORMAL)
1029 rowinfo[r].type = LT_FIRSTHEAD;
1030 endfirsthead.empty = false;
1032 endfirsthead.set = true;
1033 } else if (t.cs() == "endfoot") {
1035 endfoot.empty = true;
1037 rowinfo[row].type = LT_FOOT;
1038 for (int r = row - 1; r >= 0; --r) {
1039 if (rowinfo[r].type != LT_NORMAL)
1041 rowinfo[r].type = LT_FOOT;
1042 endfoot.empty = false;
1045 } else if (t.cs() == "endlastfoot") {
1047 endlastfoot.empty = true;
1049 rowinfo[row].type = LT_LASTFOOT;
1050 for (int r = row - 1; r >= 0; --r) {
1051 if (rowinfo[r].type != LT_NORMAL)
1053 rowinfo[r].type = LT_LASTFOOT;
1054 endlastfoot.empty = false;
1056 endlastfoot.set = true;
1057 } else if (t.cs() == "newpage") {
1060 rowinfo[row - 1].newpage = true;
1062 // This does not work in LaTeX
1068 rowinfo[row].newpage = true;
1070 cerr << "unexpected line token: " << t << endl;
1075 // LyX ends headers and footers always with \tabularnewline.
1076 // This causes one additional row in the output.
1077 // If the last row of a header/footer is empty, we can work
1078 // around that by removing it.
1080 RowInfo test = rowinfo[row-1];
1081 test.type = LT_NORMAL;
1082 if (lines[row-1].empty() && !test.special()) {
1083 switch (rowinfo[row-1].type) {
1085 if (rowinfo[row].type != LT_FIRSTHEAD &&
1086 rowinfo[row-2].type == LT_FIRSTHEAD)
1087 deletelastrow = true;
1090 if (rowinfo[row].type != LT_HEAD &&
1091 rowinfo[row-2].type == LT_HEAD)
1092 deletelastrow = true;
1095 if (rowinfo[row].type != LT_FOOT &&
1096 rowinfo[row-2].type == LT_FOOT)
1097 deletelastrow = true;
1100 if (rowinfo[row].type != LT_LASTFOOT &&
1101 rowinfo[row-2].type == LT_LASTFOOT)
1102 deletelastrow = true;
1110 if (deletelastrow) {
1111 lines.erase(lines.begin() + (row - 1));
1112 rowinfo.erase(rowinfo.begin() + (row - 1));
1113 cellinfo.erase(cellinfo.begin() + (row - 1));
1118 vector<string> cells;
1119 split(lines[row], cells, TAB);
1120 for (size_t col = 0, cell = 0; cell < cells.size();
1122 //cerr << "cell content: '" << cells[cell] << "'\n";
1123 if (col >= colinfo.size()) {
1124 // This does not work in LaTeX
1125 cerr << "Ignoring extra cell '"
1126 << cells[cell] << "'." << endl;
1129 Parser p(cells[cell]);
1131 //cells[cell] << "'\n";
1132 if (p.next_token().cs() == "multicolumn") {
1135 size_t const ncells =
1136 convert<unsigned int>(p.verbatim_item());
1138 // special cell properties alignment
1140 handle_colalign(p, t, ColInfo());
1141 p.skip_spaces(true);
1142 ColInfo & ci = t.front();
1144 // The logic of LyX for multicolumn vertical
1145 // lines is too complicated to reproduce it
1146 // here (see LyXTabular::TeXCellPreamble()).
1147 // Therefore we simply put everything in the
1151 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1152 cellinfo[row][col].align = ci.align;
1153 cellinfo[row][col].special = ci.special;
1154 cellinfo[row][col].leftlines = ci.leftlines;
1155 cellinfo[row][col].rightlines = ci.rightlines;
1157 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
1158 if (!cellinfo[row][col].content.empty()) {
1159 // This may or may not work in LaTeX,
1160 // but it does not work in LyX.
1161 // FIXME: Handle it correctly!
1162 cerr << "Moving cell content '"
1164 << "' into a multicolumn cell. "
1165 "This will probably not work."
1168 cellinfo[row][col].content += os.str();
1170 // add dummy cells for multicol
1171 for (size_t i = 0; i < ncells - 1; ++i) {
1173 if (col >= colinfo.size()) {
1174 cerr << "The cell '"
1177 << convert<string>(ncells)
1178 << " columns while the table has only "
1179 << convert<string>(colinfo.size())
1181 << " Therefore the surplus columns will be ignored."
1185 cellinfo[row][col].multi = CELL_PART_OF_MULTICOLUMN;
1186 cellinfo[row][col].align = 'c';
1189 } else if (col == 0 && colinfo.size() > 1 &&
1191 p.next_token().cs() == "caption") {
1192 // longtable caption support in LyX is a hack:
1193 // Captions require a row of their own with
1194 // the caption flag set to true, having only
1195 // one multicolumn cell. The contents of that
1196 // cell must contain exactly one caption inset
1197 // and nothing else.
1198 // Fortunately, the caption flag is only needed
1199 // for tables with more than one column.
1200 rowinfo[row].caption = true;
1201 for (size_t c = 1; c < cells.size(); ++c) {
1202 if (!cells[c].empty()) {
1203 cerr << "Moving cell content '"
1205 << "' into the caption cell. "
1206 "This will probably not work."
1208 cells[0] += cells[c];
1212 cellinfo[row][col].align = colinfo[col].align;
1213 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1215 parse_text_in_inset(p, os, FLAG_CELL, false, context);
1216 cellinfo[row][col].content += os.str();
1217 // add dummy multicolumn cells
1218 for (size_t c = 1; c < colinfo.size(); ++c)
1219 cellinfo[row][c].multi = CELL_PART_OF_MULTICOLUMN;
1223 if (p.next_token().cs() == "begin") {
1226 string const env = p.getArg('{', '}');
1227 if (env == "sideways" || env == "turn") {
1228 string angle = "90";
1229 if (env == "turn") {
1231 angle = p.getArg('{', '}');
1233 active_environments.push_back(env);
1234 p.ertEnvironment(env);
1235 active_environments.pop_back();
1237 if (!p.good() && support::isStrInt(angle))
1238 rotate = convert<int>(angle);
1242 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1243 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1244 cellinfo[row][col].align = colinfo[col].align;
1247 cellinfo[row][col].rotate = rotate;
1249 active_environments.push_back(p.getArg('{', '}'));
1252 parse_text_in_inset(p, os, FLAG_END, false, context);
1253 active_environments.pop_back();
1254 preamble.registerAutomaticallyLoadedPackage("rotating");
1256 parse_text_in_inset(p, os, FLAG_CELL, false, context);
1258 cellinfo[row][col].content += os.str();
1262 //cerr << "// handle almost empty last row what we have\n";
1263 // handle almost empty last row
1264 if (row && lines[row].empty() && row + 1 == rowinfo.size()) {
1265 //cerr << "remove empty last line\n";
1266 if (rowinfo[row].topline)
1267 rowinfo[row - 1].bottomline = true;
1268 for (size_t col = 0; col < colinfo.size(); ++col)
1269 if (cellinfo[row][col].topline)
1270 cellinfo[row - 1][col].bottomline = true;
1277 // Now we have the table structure and content in rowinfo, colinfo
1279 // Unfortunately LyX has some limitations that we need to work around.
1281 // Convert cells with special content to multicolumn cells
1282 // (LyX ignores the special field for non-multicolumn cells).
1283 for (size_t row = 0; row < rowinfo.size(); ++row) {
1284 for (size_t col = 0; col < cellinfo[row].size(); ++col) {
1285 if (cellinfo[row][col].multi == CELL_NORMAL &&
1286 !cellinfo[row][col].special.empty())
1287 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1291 // Distribute lines from rows/columns to cells
1292 // The code was stolen from convert_tablines() in lyx2lyx/lyx_1_6.py.
1293 // Each standard cell inherits the settings of the corresponding
1294 // rowinfo/colinfo. This works because all cells with individual
1295 // settings were converted to multicolumn cells above.
1296 // Each multicolumn cell inherits the settings of the rowinfo/colinfo
1297 // corresponding to the first column of the multicolumn cell (default
1298 // of the multicol package). This works because the special field
1299 // overrides the line fields.
1300 for (size_t row = 0; row < rowinfo.size(); ++row) {
1301 for (size_t col = 0; col < cellinfo[row].size(); ++col) {
1302 if (cellinfo[row][col].multi == CELL_NORMAL) {
1303 cellinfo[row][col].topline = rowinfo[row].topline;
1304 cellinfo[row][col].bottomline = rowinfo[row].bottomline;
1305 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1306 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1307 } else if (cellinfo[row][col].multi == CELL_BEGIN_OF_MULTICOLUMN) {
1309 while (s < cellinfo[row].size() &&
1310 cellinfo[row][s].multi == CELL_PART_OF_MULTICOLUMN)
1312 if (s < cellinfo[row].size() &&
1313 cellinfo[row][s].multi != CELL_BEGIN_OF_MULTICOLUMN)
1314 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1315 if (col > 0 && cellinfo[row][col-1].multi == CELL_NORMAL)
1316 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1322 preamble.registerAutomaticallyLoadedPackage("booktabs");
1323 if (is_long_tabular)
1324 preamble.registerAutomaticallyLoadedPackage("longtable");
1326 //cerr << "// output what we have\n";
1327 // output what we have
1328 string const rotate = "0";
1329 os << "\n<lyxtabular version=\"3\" rows=\"" << rowinfo.size()
1330 << "\" columns=\"" << colinfo.size() << "\">\n";
1332 << write_attribute("rotate", rotate)
1333 << write_attribute("booktabs", booktabs)
1334 << write_attribute("islongtable", is_long_tabular);
1335 if (is_long_tabular) {
1336 os << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1337 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1338 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1339 << write_attribute("headTopDL", endhead.topDL)
1340 << write_attribute("headBottomDL", endhead.bottomDL)
1341 << write_attribute("footTopDL", endfoot.topDL)
1342 << write_attribute("footBottomDL", endfoot.bottomDL)
1343 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1344 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1345 << write_attribute("lastFootEmpty", endlastfoot.empty);
1347 os << write_attribute("tabularvalignment", tabularvalignment)
1348 << write_attribute("tabularwidth", tabularwidth);
1351 //cerr << "// after header\n";
1352 for (size_t col = 0; col < colinfo.size(); ++col) {
1353 os << "<column alignment=\""
1354 << verbose_align(colinfo[col].align) << "\""
1356 << verbose_valign(colinfo[col].valign) << "\""
1357 << write_attribute("width", translate_len(colinfo[col].width))
1358 << write_attribute("special", colinfo[col].special)
1361 //cerr << "// after cols\n";
1363 for (size_t row = 0; row < rowinfo.size(); ++row) {
1365 << write_attribute("topspace", rowinfo[row].top_space)
1366 << write_attribute("bottomspace", rowinfo[row].bottom_space)
1367 << write_attribute("interlinespace", rowinfo[row].interline_space)
1368 << write_attribute("endhead",
1369 rowinfo[row].type == LT_HEAD)
1370 << write_attribute("endfirsthead",
1371 rowinfo[row].type == LT_FIRSTHEAD)
1372 << write_attribute("endfoot",
1373 rowinfo[row].type == LT_FOOT)
1374 << write_attribute("endlastfoot",
1375 rowinfo[row].type == LT_LASTFOOT)
1376 << write_attribute("newpage", rowinfo[row].newpage)
1377 << write_attribute("caption", rowinfo[row].caption)
1379 for (size_t col = 0; col < colinfo.size(); ++col) {
1380 CellInfo const & cell = cellinfo[row][col];
1382 if (cell.multi != CELL_NORMAL)
1383 os << " multicolumn=\"" << cell.multi << "\"";
1384 os << " alignment=\"" << verbose_align(cell.align)
1386 << " valignment=\"" << verbose_valign(cell.valign)
1388 << write_attribute("topline", cell.topline)
1389 << write_attribute("bottomline", cell.bottomline)
1390 << write_attribute("leftline", cell.leftlines > 0)
1391 << write_attribute("rightline", cell.rightlines > 0)
1392 << write_attribute("rotate", cell.rotate);
1393 //cerr << "\nrow: " << row << " col: " << col;
1395 // cerr << " topline=\"true\"";
1396 //if (cell.bottomline)
1397 // cerr << " bottomline=\"true\"";
1398 os << " usebox=\"none\""
1399 << write_attribute("width", translate_len(cell.width));
1400 if (cell.multi != CELL_NORMAL)
1401 os << write_attribute("special", cell.special);
1403 << "\n\\begin_inset Text\n"
1405 << "\n\\end_inset\n"
1411 os << "</lyxtabular>\n";