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), varwidth(false) {}
43 /// vertical alignment
47 /// special column alignment
49 /// number of lines on the right
51 /// 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() : set(false), 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()) {
262 string arraybackslash;
264 arraybackslash = "\\arraybackslash";
267 ci.special += ">{\\raggedright" + arraybackslash + "}";
270 ci.special += ">{\\raggedleft" + arraybackslash + "}";
273 ci.special += ">{\\centering" + arraybackslash + "}";
278 else if (ci.valign == 'n')
281 ci.special += ci.valign;
282 ci.special += '{' + ci.width + '}';
285 ci.special += ci.align;
287 // LyX can only have one left and one right line.
288 for (int i = 1; i < ci.leftlines; ++i)
289 ci.special.insert(0, "|");
290 for (int i = 1; i < ci.rightlines; ++i)
292 ci.leftlines = min(ci.leftlines, 1);
293 ci.rightlines = min(ci.rightlines, 1);
300 * Handle column specifications for tabulars and multicolumns.
301 * The next token of the parser \p p must be an opening brace, and we read
302 * everything until the matching closing brace.
303 * The resulting column specifications are filled into \p colinfo. This is
304 * in an intermediate form. fix_colalign() makes it suitable for LyX output.
306 void handle_colalign(Parser & p, vector<ColInfo> & colinfo,
307 ColInfo const & start)
309 if (p.get_token().cat() != catBegin)
310 cerr << "Wrong syntax for table column alignment.\n"
311 "Expected '{', got '" << p.curr_token().asInput()
314 ColInfo next = start;
315 for (Token t = p.get_token(); p.good() && t.cat() != catEnd;
318 cerr << "t: " << t << " c: '" << t.character() << "'\n";
321 // We cannot handle comments here
322 if (t.cat() == catComment) {
323 if (t.cs().empty()) {
327 cerr << "Ignoring comment: " << t.asInput();
331 switch (t.character()) {
333 // whitespace, ignore.
338 // new column, horizontal aligned
339 next.align = t.character();
340 if (!next.special.empty())
342 colinfo.push_back(next);
347 next.varwidth = true;
348 if (!next.special.empty())
350 colinfo.push_back(next);
356 // new column, vertical aligned box
357 next.valign = t.character();
358 next.width = p.verbatim_item();
359 if (!next.special.empty())
361 colinfo.push_back(next);
366 if (colinfo.empty()) {
367 if (next.special.empty())
371 } else if (colinfo.back().special.empty())
372 ++colinfo.back().rightlines;
373 else if (next.special.empty())
376 colinfo.back().special += '|';
379 // text before the next column
380 string const s = trimSpaceAndEol(p.verbatim_item());
381 if (next.special.empty() &&
383 // Maybe this can be converted to a
384 // horizontal alignment setting for
385 // fixed width columns
386 if (s == "\\raggedleft" || s == "\\raggedleft\\arraybackslash")
388 else if (s == "\\raggedright" || s == "\\raggedright\\arraybackslash")
390 else if (s == "\\centering" || s == "\\centering\\arraybackslash")
393 next.special = ">{" + s + '}';
395 next.special += ">{" + s + '}';
399 // text after the last column
400 string const s = trimSpaceAndEol(p.verbatim_item());
402 // This is not possible in LaTeX.
403 cerr << "Ignoring separator '<{"
404 << s << "}'." << endl;
406 ColInfo & ci = colinfo.back();
408 ci.special += "<{" + s + '}';
413 // *{n}{arg} means 'n' columns of type 'arg'
414 string const num = p.verbatim_item();
415 string const arg = p.verbatim_item();
416 size_t const n = convert<unsigned int>(num);
417 if (!arg.empty() && n > 0) {
419 for (size_t i = 0; i < n; ++i)
423 handle_colalign(p2, colinfo, next);
426 cerr << "Ignoring column specification"
427 " '*{" << num << "}{"
428 << arg << "}'." << endl;
433 // text instead of the column spacing
435 // text in addition to the column spacing
436 next.special += t.character();
437 next.special += '{' + p.verbatim_item() + '}';
440 // try user defined column types
441 // unknown column types (nargs == -1) are
442 // assumed to consume no arguments
444 next.special += t.character();
446 preamble.getSpecialTableColumnArguments(t.character());
447 for (int i = 0; i < nargs; ++i)
448 next.special += '{' +
449 p.verbatim_item() + '}';
450 colinfo.push_back(next);
457 // Maybe we have some column separators that need to be added to the
460 if (!next.special.empty()) {
461 ColInfo & ci = colinfo.back();
463 ci.special += next.special;
464 next.special.erase();
470 * Move the left and right lines and alignment settings of the column \p ci
471 * to the special field if necessary.
473 void fix_colalign(ColInfo & ci)
475 if (ci.leftlines > 1 || ci.rightlines > 1)
481 * LyX can't handle more than one vertical line at the left or right side
483 * This function moves the left and right lines and alignment settings of all
484 * columns in \p colinfo to the special field if necessary.
486 void fix_colalign(vector<ColInfo> & colinfo)
488 // Try to move extra leftlines to the previous column.
489 // We do this only if both special fields are empty, otherwise we
490 // can't tell wether the result will be the same.
491 for (size_t col = 0; col < colinfo.size(); ++col) {
492 if (colinfo[col].leftlines > 1 &&
493 colinfo[col].special.empty() && col > 0 &&
494 colinfo[col - 1].rightlines == 0 &&
495 colinfo[col - 1].special.empty()) {
496 ++colinfo[col - 1].rightlines;
497 --colinfo[col].leftlines;
500 // Try to move extra rightlines to the next column
501 for (size_t col = 0; col < colinfo.size(); ++col) {
502 if (colinfo[col].rightlines > 1 &&
503 colinfo[col].special.empty() &&
504 col < colinfo.size() - 1 &&
505 colinfo[col + 1].leftlines == 0 &&
506 colinfo[col + 1].special.empty()) {
507 ++colinfo[col + 1].leftlines;
508 --colinfo[col].rightlines;
511 // Move the lines and alignment settings to the special field if
513 for (size_t col = 0; col < colinfo.size(); ++col)
514 fix_colalign(colinfo[col]);
519 * Parse hlines and similar stuff.
520 * \returns wether the token \p t was parsed
522 bool parse_hlines(Parser & p, Token const & t, string & hlines,
523 bool is_long_tabular)
525 LASSERT(t.cat() == catEscape, return false);
527 if (t.cs() == "hline" || t.cs() == "toprule" || t.cs() == "midrule" ||
528 t.cs() == "bottomrule")
529 hlines += '\\' + t.cs();
531 else if (t.cs() == "cline")
532 hlines += "\\cline{" + p.verbatim_item() + '}';
534 else if (t.cs() == "cmidrule") {
535 // We cannot handle the \cmidrule(l){3-4} form
538 bool const hasParentheses(p.getFullArg('(', ')').first);
542 hlines += "\\cmidrule{" + p.verbatim_item() + '}';
545 else if (t.cs() == "addlinespace") {
548 bool const hasArgument(p.getFullArg('{', '}').first);
551 hlines += "\\addlinespace{" + p.verbatim_item() + '}';
553 hlines += "\\addlinespace";
556 else if (is_long_tabular && t.cs() == "newpage")
557 hlines += "\\newpage";
566 /// Position in a row
568 /// At the very beginning, before the first token
570 /// After the first token and before any column token
572 /// After the first column token. Comments and whitespace are only
573 /// treated as tokens in this position
575 /// After the first non-column token at the end
581 * Parse table structure.
582 * We parse tables in a two-pass process: This function extracts the table
583 * structure (rows, columns, hlines etc.), but does not change the cell
584 * content. The cell content is parsed in a second step in handle_tabular().
586 void parse_table(Parser & p, ostream & os, bool is_long_tabular,
587 RowPosition & pos, unsigned flags)
589 // table structure commands such as \hline
592 // comments that occur at places where we can't handle them
596 Token const & t = p.get_token();
599 debugToken(cerr, t, flags);
602 // comments and whitespace in hlines
605 case IN_HLINES_START:
607 if (t.cat() == catComment) {
612 // We can't handle comments here,
613 // store them for later use
614 comments += t.asInput();
616 } else if (t.cat() == catSpace ||
617 t.cat() == catNewline) {
618 // whitespace is irrelevant here, we
619 // need to recognize hline stuff
628 // We need to handle structure stuff first in order to
629 // determine wether we need to output a HLINE separator
630 // before the row or not.
631 if (t.cat() == catEscape) {
632 if (parse_hlines(p, t, hlines, is_long_tabular)) {
635 pos = IN_HLINES_START;
640 case IN_HLINES_START:
647 else if (t.cs() == "tabularnewline" ||
651 cerr << "Warning: Converting TeX "
652 "'\\cr' to LaTeX '\\\\'."
654 // stuff before the line break
655 os << comments << HLINE << hlines << HLINE
657 //cerr << "hlines: " << hlines << endl;
664 else if (is_long_tabular &&
665 (t.cs() == "endhead" ||
666 t.cs() == "endfirsthead" ||
667 t.cs() == "endfoot" ||
668 t.cs() == "endlastfoot")) {
669 hlines += t.asInput();
673 // these commands are implicit line
675 os << comments << HLINE << hlines
682 pos = IN_HLINES_START;
684 case IN_HLINES_START:
691 // We need a HLINE separator if we either have no hline
692 // stuff at all and are just starting a row or if we just
693 // got the first non-hline token.
696 // no hline tokens exist, first token at row start
697 case IN_HLINES_START:
698 // hline tokens exist, first non-hline token at row
700 os << hlines << HLINE << comments;
706 // Oops, there is still cell content or unsupported
707 // booktabs commands after hline stuff. The latter are
708 // moved to the cell, and the first does not work in
709 // LaTeX, so we ignore the hlines.
712 if (support::contains(hlines, "\\hline") ||
713 support::contains(hlines, "\\cline") ||
714 support::contains(hlines, "\\newpage"))
715 cerr << "Ignoring '" << hlines
716 << "' in a cell" << endl;
726 // If we come here we have normal cell content
730 if (t.cat() == catMath) {
731 // we are inside some text mode thingy, so opening new math is allowed
732 Token const & n = p.get_token();
733 if (n.cat() == catMath) {
734 // TeX's $$...$$ syntax for displayed math
736 // This does only work because parse_math outputs TeX
737 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
739 p.get_token(); // skip the second '$' token
741 // simple $...$ stuff
744 // This does only work because parse_math outputs TeX
745 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
750 else if (t.cat() == catSpace
751 || t.cat() == catNewline
752 || t.cat() == catLetter
753 || t.cat() == catSuper
755 || t.cat() == catOther
756 || t.cat() == catActive
757 || t.cat() == catParameter)
760 else if (t.cat() == catBegin) {
762 parse_table(p, os, is_long_tabular, pos,
767 else if (t.cat() == catEnd) {
768 if (flags & FLAG_BRACE_LAST)
770 cerr << "unexpected '}'\n";
773 else if (t.cat() == catAlign) {
778 else if (t.cat() == catComment)
781 else if (t.cs() == "(") {
783 // This does only work because parse_math outputs TeX
784 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
788 else if (t.cs() == "[") {
790 // This does only work because parse_math outputs TeX
791 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
795 else if (t.cs() == "begin") {
796 string const name = p.getArg('{', '}');
797 active_environments.push_back(name);
798 os << "\\begin{" << name << '}';
799 // treat the nested environment as a block, don't
800 // parse &, \\ etc, because they don't belong to our
801 // table if they appear.
802 os << p.ertEnvironment(name);
803 os << "\\end{" << name << '}';
804 active_environments.pop_back();
807 else if (t.cs() == "end") {
808 if (flags & FLAG_END) {
809 // eat environment name
810 string const name = p.getArg('{', '}');
811 if (name != active_environment())
812 p.error("\\end{" + name + "} does not match \\begin{"
813 + active_environment() + "}");
816 p.error("found 'end' unexpectedly");
823 // We can have comments if the last line is incomplete
826 // We can have hline stuff if the last line is incomplete
827 if (!hlines.empty()) {
828 // this does not work in LaTeX, so we ignore it
829 cerr << "Ignoring '" << hlines << "' at end of tabular"
835 void handle_hline_above(RowInfo & ri, vector<CellInfo> & ci)
838 for (size_t col = 0; col < ci.size(); ++col)
839 ci[col].topline = true;
843 void handle_hline_below(RowInfo & ri, vector<CellInfo> & ci)
845 ri.bottomline = true;
846 for (size_t col = 0; col < ci.size(); ++col)
847 ci[col].bottomline = true;
851 } // anonymous namespace
854 void handle_tabular(Parser & p, ostream & os, string const & name,
855 string const & tabularwidth, string const & halign,
858 bool const is_long_tabular(name == "longtable" || name == "xltabular");
859 bool booktabs = false;
860 string tabularvalignment("middle");
861 string posopts = p.getOpt();
862 if (!posopts.empty()) {
863 if (posopts == "[t]")
864 tabularvalignment = "top";
865 else if (posopts == "[b]")
866 tabularvalignment = "bottom";
868 cerr << "vertical tabular positioning '"
869 << posopts << "' ignored\n";
872 vector<ColInfo> colinfo;
874 // handle column formatting
875 handle_colalign(p, colinfo, ColInfo());
876 fix_colalign(colinfo);
878 // first scan of cells
879 // use table mode to keep it minimal-invasive
880 // not exactly what's TeX doing...
881 vector<string> lines;
883 RowPosition rowpos = ROW_START;
884 parse_table(p, ss, is_long_tabular, rowpos, FLAG_END);
885 split(ss.str(), lines, LINE);
887 vector< vector<CellInfo> > cellinfo(lines.size());
888 vector<RowInfo> rowinfo(lines.size());
895 //cerr << "// split into rows\n";
896 for (size_t row = 0; row < rowinfo.size();) {
899 cellinfo[row].resize(colinfo.size());
900 bool deletelastrow = false;
903 vector<string> dummy;
904 //cerr << "\n########### LINE: " << lines[row] << "########\n";
905 split(lines[row], dummy, HLINE);
907 // handle horizontal line fragments
908 // we do only expect this for a last line without '\\'
909 if (dummy.size() != 3) {
910 if ((dummy.size() != 1 && dummy.size() != 2) ||
911 row != rowinfo.size() - 1)
912 cerr << "unexpected dummy size: " << dummy.size()
913 << " content: " << lines[row] << "\n";
916 lines[row] = dummy[1];
918 //cerr << "line: " << row << " above 0: " << dummy[0] << "\n";
919 //cerr << "line: " << row << " below 2: " << dummy[2] << "\n";
920 //cerr << "line: " << row << " cells 1: " << dummy[1] << "\n";
922 for (int i = 0; i <= 2; i += 2) {
923 //cerr << " reading from line string '" << dummy[i] << "'\n";
926 Token t = p1.get_token();
927 //cerr << "read token: " << t << "\n";
928 if (t.cs() == "hline" || t.cs() == "toprule" ||
929 t.cs() == "midrule" ||
930 t.cs() == "bottomrule") {
931 if (t.cs() != "hline")
934 if (rowinfo[row].topline) {
935 if (row > 0) // extra bottomline above
936 handle_hline_below(rowinfo[row - 1], cellinfo[row - 1]);
938 cerr << "dropping extra "
940 //cerr << "below row: " << row-1 << endl;
942 handle_hline_above(rowinfo[row], cellinfo[row]);
943 //cerr << "above row: " << row << endl;
946 //cerr << "below row: " << row << endl;
947 handle_hline_below(rowinfo[row], cellinfo[row]);
949 } else if (t.cs() == "cline" || t.cs() == "cmidrule") {
950 if (t.cs() == "cmidrule")
952 string arg = p1.verbatim_item();
953 //cerr << "read " << t.cs() << " arg: '" << arg << "'\n";
955 split(arg, cols, '-');
957 size_t from = convert<unsigned int>(cols[0]);
959 cerr << "Could not parse "
960 << t.cs() << " start column."
963 // 1 based index -> 0 based
965 if (from >= colinfo.size()) {
966 cerr << t.cs() << " starts at "
967 "non existing column "
968 << (from + 1) << endl;
969 from = colinfo.size() - 1;
971 size_t to = convert<unsigned int>(cols[1]);
973 cerr << "Could not parse "
974 << t.cs() << " end column."
977 // 1 based index -> 0 based
979 if (to >= colinfo.size()) {
980 cerr << t.cs() << " ends at "
981 "non existing column "
983 to = colinfo.size() - 1;
985 for (size_t col = from; col <= to; ++col) {
986 //cerr << "row: " << row << " col: " << col << " i: " << i << endl;
988 rowinfo[row].topline = true;
989 cellinfo[row][col].topline = true;
991 rowinfo[row].bottomline = true;
992 cellinfo[row][col].bottomline = true;
995 } else if (t.cs() == "addlinespace") {
997 string const opt = p.next_token().cat() == catBegin ?
998 p.verbatim_item() : string();
1001 rowinfo[row].top_space = "default";
1003 rowinfo[row].top_space = translate_len(opt);
1004 } else if (rowinfo[row].bottomline) {
1006 rowinfo[row].bottom_space = "default";
1008 rowinfo[row].bottom_space = translate_len(opt);
1011 rowinfo[row].interline_space = "default";
1013 rowinfo[row].interline_space = translate_len(opt);
1015 } else if (t.cs() == "endhead") {
1017 endhead.empty = true;
1019 rowinfo[row].type = LT_HEAD;
1020 for (int r = row - 1; r >= 0; --r) {
1021 if (rowinfo[r].type != LT_NORMAL)
1023 rowinfo[r].type = LT_HEAD;
1024 endhead.empty = false;
1027 } else if (t.cs() == "endfirsthead") {
1029 endfirsthead.empty = true;
1031 rowinfo[row].type = LT_FIRSTHEAD;
1032 for (int r = row - 1; r >= 0; --r) {
1033 if (rowinfo[r].type != LT_NORMAL)
1035 rowinfo[r].type = LT_FIRSTHEAD;
1036 endfirsthead.empty = false;
1038 endfirsthead.set = true;
1039 } else if (t.cs() == "endfoot") {
1041 endfoot.empty = true;
1043 rowinfo[row].type = LT_FOOT;
1044 for (int r = row - 1; r >= 0; --r) {
1045 if (rowinfo[r].type != LT_NORMAL)
1047 rowinfo[r].type = LT_FOOT;
1048 endfoot.empty = false;
1051 } else if (t.cs() == "endlastfoot") {
1053 endlastfoot.empty = true;
1055 rowinfo[row].type = LT_LASTFOOT;
1056 for (int r = row - 1; r >= 0; --r) {
1057 if (rowinfo[r].type != LT_NORMAL)
1059 rowinfo[r].type = LT_LASTFOOT;
1060 endlastfoot.empty = false;
1062 endlastfoot.set = true;
1063 } else if (t.cs() == "newpage") {
1066 rowinfo[row - 1].newpage = true;
1068 // This does not work in LaTeX
1074 rowinfo[row].newpage = true;
1076 cerr << "unexpected line token: " << t << endl;
1081 // LyX ends headers and footers always with \tabularnewline.
1082 // This causes one additional row in the output.
1083 // If the last row of a header/footer is empty, we can work
1084 // around that by removing it.
1086 RowInfo test = rowinfo[row-1];
1087 test.type = LT_NORMAL;
1088 if (lines[row-1].empty() && !test.special()) {
1089 switch (rowinfo[row-1].type) {
1091 if (rowinfo[row].type != LT_FIRSTHEAD &&
1092 rowinfo[row-2].type == LT_FIRSTHEAD)
1093 deletelastrow = true;
1096 if (rowinfo[row].type != LT_HEAD &&
1097 rowinfo[row-2].type == LT_HEAD)
1098 deletelastrow = true;
1101 if (rowinfo[row].type != LT_FOOT &&
1102 rowinfo[row-2].type == LT_FOOT)
1103 deletelastrow = true;
1106 if (rowinfo[row].type != LT_LASTFOOT &&
1107 rowinfo[row-2].type == LT_LASTFOOT)
1108 deletelastrow = true;
1116 if (deletelastrow) {
1117 lines.erase(lines.begin() + (row - 1));
1118 rowinfo.erase(rowinfo.begin() + (row - 1));
1119 cellinfo.erase(cellinfo.begin() + (row - 1));
1124 vector<string> cells;
1125 split(lines[row], cells, TAB);
1126 for (size_t col = 0, cell = 0; cell < cells.size();
1128 //cerr << "cell content: '" << cells[cell] << "'\n";
1129 if (col >= colinfo.size()) {
1130 // This does not work in LaTeX
1131 cerr << "Ignoring extra cell '"
1132 << cells[cell] << "'." << endl;
1135 Parser parse(cells[cell]);
1136 parse.skip_spaces();
1137 //cells[cell] << "'\n";
1138 if (parse.next_token().cs() == "multicolumn") {
1141 size_t const ncells =
1142 convert<unsigned int>(parse.verbatim_item());
1144 // special cell properties alignment
1146 handle_colalign(parse, t, ColInfo());
1147 parse.skip_spaces(true);
1148 ColInfo & ci = t.front();
1150 // The logic of LyX for multicolumn vertical
1151 // lines is too complicated to reproduce it
1152 // here (see LyXTabular::TeXCellPreamble()).
1153 // Therefore we simply put everything in the
1157 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1158 cellinfo[row][col].align = ci.align;
1159 cellinfo[row][col].special = ci.special;
1160 cellinfo[row][col].leftlines = ci.leftlines;
1161 cellinfo[row][col].rightlines = ci.rightlines;
1163 parse_text_in_inset(parse, os2, FLAG_ITEM, false, context);
1164 if (!cellinfo[row][col].content.empty()) {
1165 // This may or may not work in LaTeX,
1166 // but it does not work in LyX.
1167 // FIXME: Handle it correctly!
1168 cerr << "Moving cell content '"
1170 << "' into a multicolumn cell. "
1171 "This will probably not work."
1174 cellinfo[row][col].content += os2.str();
1176 // add dummy cells for multicol
1177 for (size_t i = 0; i < ncells - 1; ++i) {
1179 if (col >= colinfo.size()) {
1180 cerr << "The cell '"
1183 << convert<string>(ncells)
1184 << " columns while the table has only "
1185 << convert<string>(colinfo.size())
1187 << " Therefore the surplus columns will be ignored."
1191 cellinfo[row][col].multi = CELL_PART_OF_MULTICOLUMN;
1192 cellinfo[row][col].align = 'c';
1195 } else if (col == 0 && colinfo.size() > 1 &&
1197 parse.next_token().cs() == "caption") {
1198 // longtable caption support in LyX is a hack:
1199 // Captions require a row of their own with
1200 // the caption flag set to true, having only
1201 // one multicolumn cell. The contents of that
1202 // cell must contain exactly one caption inset
1203 // and nothing else.
1204 // Fortunately, the caption flag is only needed
1205 // for tables with more than one column.
1206 rowinfo[row].caption = true;
1207 for (size_t c = 1; c < cells.size(); ++c) {
1208 if (!cells[c].empty()) {
1209 cerr << "Moving cell content '"
1211 << "' into the caption cell. "
1212 "This will probably not work."
1214 cells[0] += cells[c];
1218 cellinfo[row][col].align = colinfo[col].align;
1219 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1221 parse_text_in_inset(parse, os2, FLAG_CELL, false, context);
1222 cellinfo[row][col].content += os2.str();
1223 // add dummy multicolumn cells
1224 for (size_t c = 1; c < colinfo.size(); ++c)
1225 cellinfo[row][c].multi = CELL_PART_OF_MULTICOLUMN;
1229 if (parse.next_token().cs() == "begin") {
1230 parse.pushPosition();
1232 string const env = parse.getArg('{', '}');
1233 if (env == "sideways" || env == "turn") {
1234 string angle = "90";
1235 if (env == "turn") {
1237 angle = parse.getArg('{', '}');
1239 active_environments.push_back(env);
1240 parse.ertEnvironment(env);
1241 active_environments.pop_back();
1242 parse.skip_spaces();
1243 if (!parse.good() && support::isStrInt(angle))
1244 rotate = convert<int>(angle);
1246 parse.popPosition();
1248 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1249 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1250 cellinfo[row][col].align = colinfo[col].align;
1253 cellinfo[row][col].rotate = rotate;
1255 active_environments.push_back(parse.getArg('{', '}'));
1257 parse.getArg('{', '}');
1258 parse_text_in_inset(parse, os2, FLAG_END, false, context);
1259 active_environments.pop_back();
1260 preamble.registerAutomaticallyLoadedPackage("rotating");
1262 parse_text_in_inset(parse, os2, FLAG_CELL, false, context);
1264 cellinfo[row][col].content += os2.str();
1268 //cerr << "// handle almost empty last row what we have\n";
1269 // handle almost empty last row
1270 if (row && lines[row].empty() && row + 1 == rowinfo.size()) {
1271 //cerr << "remove empty last line\n";
1272 if (rowinfo[row].topline)
1273 rowinfo[row - 1].bottomline = true;
1274 for (size_t col = 0; col < colinfo.size(); ++col)
1275 if (cellinfo[row][col].topline)
1276 cellinfo[row - 1][col].bottomline = true;
1283 // Now we have the table structure and content in rowinfo, colinfo
1285 // Unfortunately LyX has some limitations that we need to work around.
1287 // Convert cells with special content to multicolumn cells
1288 // (LyX ignores the special field for non-multicolumn cells).
1289 for (size_t row = 0; row < rowinfo.size(); ++row) {
1290 for (size_t col = 0; col < cellinfo[row].size(); ++col) {
1291 if (cellinfo[row][col].multi == CELL_NORMAL &&
1292 !cellinfo[row][col].special.empty())
1293 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1297 // Distribute lines from rows/columns to cells
1298 // The code was stolen from convert_tablines() in lyx2lyx/lyx_1_6.py.
1299 // Each standard cell inherits the settings of the corresponding
1300 // rowinfo/colinfo. This works because all cells with individual
1301 // settings were converted to multicolumn cells above.
1302 // Each multicolumn cell inherits the settings of the rowinfo/colinfo
1303 // corresponding to the first column of the multicolumn cell (default
1304 // of the multicol package). This works because the special field
1305 // overrides the line fields.
1306 for (size_t row = 0; row < rowinfo.size(); ++row) {
1307 for (size_t col = 0; col < cellinfo[row].size(); ++col) {
1308 if (cellinfo[row][col].multi == CELL_NORMAL) {
1309 cellinfo[row][col].topline = rowinfo[row].topline;
1310 cellinfo[row][col].bottomline = rowinfo[row].bottomline;
1311 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1312 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1313 } else if (cellinfo[row][col].multi == CELL_BEGIN_OF_MULTICOLUMN) {
1315 while (s < cellinfo[row].size() &&
1316 cellinfo[row][s].multi == CELL_PART_OF_MULTICOLUMN)
1318 if (s < cellinfo[row].size() &&
1319 cellinfo[row][s].multi != CELL_BEGIN_OF_MULTICOLUMN)
1320 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1321 if (col > 0 && cellinfo[row][col-1].multi == CELL_NORMAL)
1322 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1328 preamble.registerAutomaticallyLoadedPackage("booktabs");
1329 if (name == "longtable")
1330 preamble.registerAutomaticallyLoadedPackage("longtable");
1331 else if (name == "xltabular")
1332 preamble.registerAutomaticallyLoadedPackage("xltabular");
1333 else if (name == "tabularx")
1334 preamble.registerAutomaticallyLoadedPackage("tabularx");
1336 //cerr << "// output what we have\n";
1337 // output what we have
1338 string const rotate = "0";
1339 os << "\n<lyxtabular version=\"3\" rows=\"" << rowinfo.size()
1340 << "\" columns=\"" << colinfo.size() << "\">\n";
1342 << write_attribute("rotate", rotate)
1343 << write_attribute("booktabs", booktabs)
1344 << write_attribute("islongtable", is_long_tabular);
1345 if (is_long_tabular) {
1346 os << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1347 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1348 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1349 << write_attribute("headTopDL", endhead.topDL)
1350 << write_attribute("headBottomDL", endhead.bottomDL)
1351 << write_attribute("footTopDL", endfoot.topDL)
1352 << write_attribute("footBottomDL", endfoot.bottomDL)
1353 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1354 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1355 << write_attribute("lastFootEmpty", endlastfoot.empty);
1356 if (!halign.empty())
1357 os << write_attribute("longtabularalignment", halign);
1359 os << write_attribute("tabularvalignment", tabularvalignment)
1360 << write_attribute("tabularwidth", tabularwidth);
1363 //cerr << "// after header\n";
1364 for (size_t col = 0; col < colinfo.size(); ++col) {
1365 os << "<column alignment=\""
1366 << verbose_align(colinfo[col].align) << "\""
1368 << verbose_valign(colinfo[col].valign) << "\""
1369 << write_attribute("width", translate_len(colinfo[col].width))
1370 << write_attribute("special", colinfo[col].special)
1371 << write_attribute("varwidth", colinfo[col].varwidth)
1374 //cerr << "// after cols\n";
1376 for (size_t row = 0; row < rowinfo.size(); ++row) {
1378 << write_attribute("topspace", rowinfo[row].top_space)
1379 << write_attribute("bottomspace", rowinfo[row].bottom_space)
1380 << write_attribute("interlinespace", rowinfo[row].interline_space)
1381 << write_attribute("endhead",
1382 rowinfo[row].type == LT_HEAD)
1383 << write_attribute("endfirsthead",
1384 rowinfo[row].type == LT_FIRSTHEAD)
1385 << write_attribute("endfoot",
1386 rowinfo[row].type == LT_FOOT)
1387 << write_attribute("endlastfoot",
1388 rowinfo[row].type == LT_LASTFOOT)
1389 << write_attribute("newpage", rowinfo[row].newpage)
1390 << write_attribute("caption", rowinfo[row].caption)
1392 for (size_t col = 0; col < colinfo.size(); ++col) {
1393 CellInfo const & cell = cellinfo[row][col];
1395 if (cell.multi != CELL_NORMAL)
1396 os << " multicolumn=\"" << cell.multi << "\"";
1397 os << " alignment=\"" << verbose_align(cell.align)
1399 << " valignment=\"" << verbose_valign(cell.valign)
1401 << write_attribute("topline", cell.topline)
1402 << write_attribute("bottomline", cell.bottomline)
1403 << write_attribute("leftline", cell.leftlines > 0)
1404 << write_attribute("rightline", cell.rightlines > 0)
1405 << write_attribute("rotate", cell.rotate);
1406 //cerr << "\nrow: " << row << " col: " << col;
1408 // cerr << " topline=\"true\"";
1409 //if (cell.bottomline)
1410 // cerr << " bottomline=\"true\"";
1411 os << " usebox=\"none\""
1412 << write_attribute("width", translate_len(cell.width));
1413 if (cell.multi != CELL_NORMAL)
1414 os << write_attribute("special", cell.special);
1416 << "\n\\begin_inset Text\n"
1418 << "\n\\end_inset\n"
1424 os << "</lyxtabular>\n";