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.
22 #include "support/lassert.h"
23 #include "support/convert.h"
24 #include "support/lstrings.h"
41 ColInfo() : align('n'), valign('n'), rightlines(0), leftlines(0),
42 varwidth(false), decimal_point('\0'), vcolumn(false) {}
45 /// vertical alignment
49 /// special column alignment
51 /// number of lines on the right
53 /// number of lines on the left
64 /// row type for longtables
71 /// part of head on first page
75 /// part of foot on last page
82 RowInfo() : topline(false), bottomline(false), type(LT_NORMAL),
83 caption(false), newpage(false) {}
84 /// Does this row have any special setting?
87 return topline || bottomline || !top_space.empty() ||
88 !bottom_space.empty() || !interline_space.empty() ||
89 type != LT_NORMAL || caption || newpage;
91 /// horizontal line above
93 /// horizontal line below
95 /// Extra space between the top line and this row
97 /// Extra space between this row and the bottom line
99 /// Extra space between the bottom line and the next top line
100 string interline_space;
101 /// These are for longtabulars only
102 /// row type (head, foot, firsthead etc.)
104 /// row for a caption
106 /// row for a newpage
111 /// the numeric values are part of the file format!
115 /// A multicolumn cell. The number of columns is <tt>1 + number
116 /// of CELL_PART_OF_MULTICOLUMN cells</tt> that follow directly
117 CELL_BEGIN_OF_MULTICOLUMN,
118 /// This is a dummy cell (part of a multicolumn cell)
119 CELL_PART_OF_MULTICOLUMN,
121 CELL_BEGIN_OF_MULTIROW,
123 CELL_PART_OF_MULTIROW
129 CellInfo() : multi(CELL_NORMAL), align('n'), valign('n'),
130 leftlines(0), rightlines(0), topline(false),
131 bottomline(false), rotate(0), mrxnum(0) {}
138 /// vertical cell alignment
140 /// number of lines on the left
142 /// number of lines on the right
144 /// do we have a line above?
146 /// do we have a line below?
148 /// how is the cell rotated?
150 /// width for multicolumn cells
152 /// special formatting for multicolumn cells
156 /// number of further multirows
164 ltType() : set(false), topDL(false), bottomDL(false), empty(false) {}
165 // we have this header type (is set in the getLT... functions)
167 // double borders on top
169 // double borders on bottom
171 // used for FirstHeader & LastFooter and if this is true
172 // all the rows marked as FirstHeader or LastFooter are
173 // ignored in the output and it is set to be empty!
178 /// translate a horizontal alignment (as stored in ColInfo and CellInfo) to LyX
179 inline char const * verbose_align(char c)
196 /// translate a vertical alignment (as stored in ColInfo and CellInfo) to LyX
197 inline char const * verbose_valign(char c)
199 // The default value for no special alignment is "top".
212 // stripped down from tabluar.C. We use it currently only for bools and
214 string const write_attribute(string const & name, bool const & b)
216 // we write only true attribute values so we remove a bit of the
217 // file format bloat for tabulars.
218 return b ? ' ' + name + "=\"true\"" : string();
222 string const write_attribute(string const & name, string const & s)
224 return s.empty() ? string() : ' ' + name + "=\"" + s + '"';
228 string const write_attribute(string const & name, int const & i)
230 // we write only true attribute values so we remove a bit of the
231 // file format bloat for tabulars.
232 return i ? write_attribute(name, convert<string>(i)) : string();
236 /*! rather brutish way to code table structure in a string:
241 \multicolumn{2}{c}{4} & 5 //
247 gets "translated" to:
250 HLINE 1 TAB 2 TAB 3 HLINE HLINE LINE
251 \hline HLINE \multicolumn{2}{c}{4} TAB 5 HLINE HLINE LINE
252 HLINE 6 TAB 7 HLINE HLINE LINE
253 HLINE 8 HLINE \endhead HLINE LINE
257 char const TAB = '\001';
258 char const LINE = '\002';
259 char const HLINE = '\004';
263 * Move the information in leftlines, rightlines, align and valign to the
264 * special field. This is necessary if the special field is not empty,
265 * because LyX ignores leftlines > 1, rightlines > 1, align and valign in
268 void ci2special(ColInfo & ci)
270 if (ci.width.empty() && ci.align == 'n')
271 // The alignment setting is already in special, since
272 // handle_colalign() never stores ci with these settings
273 // and ensures that leftlines == 0 and rightlines == 0 in
277 if (ci.decimal_point != '\0') {
278 // we only support decimal point natively
279 // with 'l' alignment in or 'n' alignment
280 // with width in second row
281 if (ci.align != 'l' && ci.align != 'n') {
282 ci.decimal_point = '\0';
288 if (!ci.width.empty()) {
289 string arraybackslash;
291 arraybackslash = "\\arraybackslash";
294 ci.special += ">{\\raggedright" + arraybackslash + "}";
297 ci.special += ">{\\raggedleft" + arraybackslash + "}";
300 ci.special += ">{\\centering" + arraybackslash + "}";
305 else if (ci.varwidth)
307 else if (ci.valign == 'n')
310 ci.special += ci.valign;
311 ci.special += '{' + ci.width + '}';
314 ci.special += ci.align;
316 // LyX can only have one left and one right line.
317 for (int i = 1; i < ci.leftlines; ++i)
318 ci.special.insert(0, "|");
319 for (int i = 1; i < ci.rightlines; ++i)
321 ci.leftlines = min(ci.leftlines, 1);
322 ci.rightlines = min(ci.rightlines, 1);
329 * Handle column specifications for tabulars and multicolumns.
330 * The next token of the parser \p p must be an opening brace, and we read
331 * everything until the matching closing brace.
332 * The resulting column specifications are filled into \p colinfo. This is
333 * in an intermediate form. fix_colalign() makes it suitable for LyX output.
335 void handle_colalign(Parser & p, vector<ColInfo> & colinfo,
336 ColInfo const & start)
338 if (p.get_token().cat() != catBegin)
339 cerr << "Wrong syntax for table column alignment.\n"
340 "Expected '{', got '" << p.curr_token().asInput()
343 ColInfo next = start;
344 for (Token t = p.get_token(); p.good() && t.cat() != catEnd;
347 cerr << "t: " << t << " c: '" << t.character() << "'\n";
350 // We cannot handle comments here
351 if (t.cat() == catComment) {
352 if (t.cs().empty()) {
356 cerr << "Ignoring comment: " << t.asInput();
360 switch (t.character()) {
362 // whitespace, ignore.
367 // new column, horizontal aligned
368 next.align = t.character();
369 if (!next.special.empty()) {
371 // handle decimal separator
372 if (next.decimal_point != '\0') {
373 if (!colinfo.empty() && colinfo.back().align == 'r') {
374 colinfo.back().align = 'd';
375 colinfo.back().decimal_point = next.decimal_point;
377 next.decimal_point = '\0';
380 colinfo.push_back(next);
385 next.varwidth = true;
386 if (!next.special.empty())
388 colinfo.push_back(next);
392 // V column type (varwidth package)
393 string const s = trimSpaceAndEol(p.verbatim_item());
394 // V{\linewidth} is treated as a normal column
395 // (which allows for line breaks). The V type is
396 // automatically set by LyX in this case
397 if (s != "\\linewidth" || !next.special.empty()) {
402 colinfo.push_back(next);
409 // new column, vertical aligned box
410 next.valign = t.character();
411 next.width = p.verbatim_item();
412 if (!next.special.empty()) {
414 // handle decimal separator
415 if (next.decimal_point != '\0') {
416 if (!colinfo.empty() && colinfo.back().align == 'r') {
417 colinfo.back().align = 'd';
418 colinfo.back().decimal_point = next.decimal_point;
420 next.decimal_point = '\0';
423 colinfo.push_back(next);
428 if (colinfo.empty()) {
429 if (next.special.empty())
433 } else if (colinfo.back().special.empty())
434 ++colinfo.back().rightlines;
435 else if (next.special.empty())
438 colinfo.back().special += '|';
441 // text before the next column
442 string const s = trimSpaceAndEol(p.verbatim_item());
443 if (next.special.empty() &&
445 // Maybe this can be converted to a
446 // horizontal alignment setting for
447 // fixed width columns
448 if (s == "\\raggedleft" || s == "\\raggedleft\\arraybackslash")
450 else if (s == "\\raggedright" || s == "\\raggedright\\arraybackslash")
452 else if (s == "\\centering" || s == "\\centering\\arraybackslash")
455 next.special = ">{" + s + '}';
457 next.special += ">{" + s + '}';
461 // text after the last column
462 string const s = trimSpaceAndEol(p.verbatim_item());
464 // This is not possible in LaTeX.
465 cerr << "Ignoring separator '<{"
466 << s << "}'." << endl;
468 ColInfo & ci = colinfo.back();
470 ci.special += "<{" + s + '}';
475 if (p.next_token().character() != '{')
477 // *{n}{arg} means 'n' columns of type 'arg'
478 string const num = p.verbatim_item();
479 string const arg = p.verbatim_item();
480 size_t const n = convert<unsigned int>(num);
481 if (!arg.empty() && n > 0) {
483 for (size_t i = 0; i < n; ++i)
487 handle_colalign(p2, colinfo, next);
490 cerr << "Ignoring column specification"
491 " '*{" << num << "}{"
492 << arg << "}'." << endl;
497 // text instead of the column spacing
499 // text in addition to the column spacing
500 string const arg = p.verbatim_item();
501 next.special += t.character();
502 next.special += '{' + arg + '}';
503 string const sarg = arg.size() > 2 ? arg.substr(0, arg.size() - 1) : string();
504 if (t.character() == '@' && sarg == "\\extracolsep{0pt}")
505 next.decimal_point = arg.back();
509 // try user defined column types
510 // unknown column types (nargs == -1) are
511 // assumed to consume no arguments
513 next.special += t.character();
515 preamble.getSpecialTableColumnArguments(t.character());
516 for (int i = 0; i < nargs; ++i)
517 next.special += '{' +
518 p.verbatim_item() + '}';
519 colinfo.push_back(next);
526 // Maybe we have some column separators that need to be added to the
529 if (!next.special.empty()) {
530 ColInfo & ci = colinfo.back();
532 ci.special += next.special;
533 next.special.erase();
539 * Move the left and right lines and alignment settings of the column \p ci
540 * to the special field if necessary.
542 void fix_colalign(ColInfo & ci)
544 if (ci.leftlines > 1 || ci.rightlines > 1)
550 * LyX can't handle more than one vertical line at the left or right side
552 * This function moves the left and right lines and alignment settings of all
553 * columns in \p colinfo to the special field if necessary.
555 void fix_colalign(vector<ColInfo> & colinfo)
557 // Try to move extra leftlines to the previous column.
558 // We do this only if both special fields are empty, otherwise we
559 // can't tell wether the result will be the same.
560 for (size_t col = 0; col < colinfo.size(); ++col) {
561 if (colinfo[col].leftlines > 1 &&
562 colinfo[col].special.empty() && col > 0 &&
563 colinfo[col - 1].rightlines == 0 &&
564 colinfo[col - 1].special.empty()) {
565 ++colinfo[col - 1].rightlines;
566 --colinfo[col].leftlines;
569 // Try to move extra rightlines to the next column
570 for (size_t col = 0; col < colinfo.size(); ++col) {
571 if (colinfo[col].rightlines > 1 &&
572 colinfo[col].special.empty() &&
573 col < colinfo.size() - 1 &&
574 colinfo[col + 1].leftlines == 0 &&
575 colinfo[col + 1].special.empty()) {
576 ++colinfo[col + 1].leftlines;
577 --colinfo[col].rightlines;
580 // Move the lines and alignment settings to the special field if
582 for (size_t col = 0; col < colinfo.size(); ++col)
583 fix_colalign(colinfo[col]);
588 * Parse hlines and similar stuff.
589 * \returns wether the token \p t was parsed
591 bool parse_hlines(Parser & p, Token const & t, string & hlines,
592 bool is_long_tabular)
594 LASSERT(t.cat() == catEscape, return false);
596 if (t.cs() == "hline" || t.cs() == "toprule" || t.cs() == "midrule" ||
597 t.cs() == "bottomrule")
598 hlines += '\\' + t.cs();
600 else if (t.cs() == "cline")
601 hlines += "\\cline{" + p.verbatim_item() + '}';
603 else if (t.cs() == "cmidrule") {
604 // We cannot handle the \cmidrule(l){3-4} form
607 bool const hasParentheses(p.getFullArg('(', ')').first);
611 hlines += "\\cmidrule{" + p.verbatim_item() + '}';
614 else if (t.cs() == "addlinespace") {
617 bool const hasArgument(p.getFullArg('{', '}').first);
620 hlines += "\\addlinespace{" + p.verbatim_item() + '}';
622 hlines += "\\addlinespace";
625 else if (is_long_tabular && t.cs() == "newpage")
626 hlines += "\\newpage";
635 /// Position in a row
637 /// At the very beginning, before the first token
639 /// After the first token and before any column token
641 /// After the first column token. Comments and whitespace are only
642 /// treated as tokens in this position
644 /// After the first non-column token at the end
650 * Parse table structure.
651 * We parse tables in a two-pass process: This function extracts the table
652 * structure (rows, columns, hlines etc.), but does not change the cell
653 * content. The cell content is parsed in a second step in handle_tabular().
655 void parse_table(Parser & p, ostream & os, bool is_long_tabular,
656 RowPosition & pos, unsigned flags)
658 // table structure commands such as \hline
661 // comments that occur at places where we can't handle them
665 Token const & t = p.get_token();
668 debugToken(cerr, t, flags);
671 // comments and whitespace in hlines
674 case IN_HLINES_START:
676 if (t.cat() == catComment) {
681 // We can't handle comments here,
682 // store them for later use
683 comments += t.asInput();
685 } else if (t.cat() == catSpace ||
686 t.cat() == catNewline) {
687 // whitespace is irrelevant here, we
688 // need to recognize hline stuff
697 // We need to handle structure stuff first in order to
698 // determine wether we need to output a HLINE separator
699 // before the row or not.
700 if (t.cat() == catEscape) {
701 if (parse_hlines(p, t, hlines, is_long_tabular)) {
704 pos = IN_HLINES_START;
709 case IN_HLINES_START:
716 else if (t.cs() == "tabularnewline" ||
720 cerr << "Warning: Converting TeX "
721 "'\\cr' to LaTeX '\\\\'."
723 // stuff before the line break
724 os << comments << HLINE << hlines << HLINE
726 //cerr << "hlines: " << hlines << endl;
733 else if (is_long_tabular &&
734 (t.cs() == "endhead" ||
735 t.cs() == "endfirsthead" ||
736 t.cs() == "endfoot" ||
737 t.cs() == "endlastfoot")) {
738 hlines += t.asInput();
742 // these commands are implicit line
744 os << comments << HLINE << hlines
751 pos = IN_HLINES_START;
753 case IN_HLINES_START:
760 // We need a HLINE separator if we either have no hline
761 // stuff at all and are just starting a row or if we just
762 // got the first non-hline token.
765 // no hline tokens exist, first token at row start
766 case IN_HLINES_START:
767 // hline tokens exist, first non-hline token at row
769 os << hlines << HLINE << comments;
775 // Oops, there is still cell content or unsupported
776 // booktabs commands after hline stuff. The latter are
777 // moved to the cell, and the first does not work in
778 // LaTeX, so we ignore the hlines.
781 if (support::contains(hlines, "\\hline") ||
782 support::contains(hlines, "\\cline") ||
783 support::contains(hlines, "\\newpage"))
784 cerr << "Ignoring '" << hlines
785 << "' in a cell" << endl;
795 // If we come here we have normal cell content
799 if (t.cat() == catMath) {
800 // we are inside some text mode thingy, so opening new math is allowed
801 Token const & n = p.get_token();
802 if (n.cat() == catMath) {
803 // TeX's $$...$$ syntax for displayed math
805 // This does only work because parse_math outputs TeX
806 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
808 p.get_token(); // skip the second '$' token
810 // simple $...$ stuff
813 // This does only work because parse_math outputs TeX
814 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
819 else if (t.cat() == catSpace
820 || t.cat() == catNewline
821 || t.cat() == catLetter
822 || t.cat() == catSuper
824 || t.cat() == catOther
825 || t.cat() == catActive
826 || t.cat() == catParameter)
829 else if (t.cat() == catBegin) {
831 parse_table(p, os, is_long_tabular, pos,
836 else if (t.cat() == catEnd) {
837 if (flags & FLAG_BRACE_LAST)
839 cerr << "unexpected '}'\n";
842 else if (t.cat() == catAlign) {
847 else if (t.cat() == catComment)
850 else if (t.cs() == "(") {
852 // This does only work because parse_math outputs TeX
853 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
857 else if (t.cs() == "[") {
859 // This does only work because parse_math outputs TeX
860 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
864 else if (t.cs() == "begin") {
865 string const name = p.getArg('{', '}');
866 active_environments.push_back(name);
867 os << "\\begin{" << name << '}';
868 // treat the nested environment as a block, don't
869 // parse &, \\ etc, because they don't belong to our
870 // table if they appear.
871 os << p.ertEnvironment(name);
872 os << "\\end{" << name << '}';
873 active_environments.pop_back();
876 else if (t.cs() == "end") {
877 if (flags & FLAG_END) {
878 // eat environment name
879 string const name = p.getArg('{', '}');
880 if (name != active_environment())
881 p.error("\\end{" + name + "} does not match \\begin{"
882 + active_environment() + "}");
885 p.error("found 'end' unexpectedly");
892 // We can have comments if the last line is incomplete
895 // We can have hline stuff if the last line is incomplete
896 if (!hlines.empty()) {
897 // this does not work in LaTeX, so we ignore it
898 cerr << "Ignoring '" << hlines << "' at end of tabular"
904 void handle_hline_above(RowInfo & ri, vector<CellInfo> & ci)
907 for (size_t col = 0; col < ci.size(); ++col)
908 ci[col].topline = true;
912 void handle_hline_below(RowInfo & ri, vector<CellInfo> & ci)
914 ri.bottomline = true;
915 for (size_t col = 0; col < ci.size(); ++col)
916 ci[col].bottomline = true;
920 } // anonymous namespace
923 void handle_tabular(Parser & p, ostream & os, string const & name,
924 string const & tabularwidth, string const & halign,
927 bool const is_long_tabular(name == "longtable" || name == "xltabular");
928 bool booktabs = false;
929 string tabularvalignment("middle");
930 string posopts = p.getOpt();
931 if (!posopts.empty()) {
932 if (posopts == "[t]")
933 tabularvalignment = "top";
934 else if (posopts == "[b]")
935 tabularvalignment = "bottom";
937 cerr << "vertical tabular positioning '"
938 << posopts << "' ignored\n";
941 vector<ColInfo> colinfo;
943 // handle column formatting
944 handle_colalign(p, colinfo, ColInfo());
945 fix_colalign(colinfo);
947 // first scan of cells
948 // use table mode to keep it minimal-invasive
949 // not exactly what's TeX doing...
950 vector<string> lines;
952 RowPosition rowpos = ROW_START;
953 parse_table(p, ss, is_long_tabular, rowpos, FLAG_END);
954 split(ss.str(), lines, LINE);
956 vector< vector<CellInfo> > cellinfo(lines.size());
957 vector<RowInfo> rowinfo(lines.size());
964 //cerr << "// split into rows\n";
965 for (size_t row = 0; row < rowinfo.size();) {
968 cellinfo[row].resize(colinfo.size());
969 bool deletelastrow = false;
972 vector<string> dummy;
973 //cerr << "\n########### LINE: " << lines[row] << "########\n";
974 split(lines[row], dummy, HLINE);
976 // handle horizontal line fragments
977 // we do only expect this for a last line without '\\'
978 if (dummy.size() != 3) {
979 if ((dummy.size() != 1 && dummy.size() != 2) ||
980 row != rowinfo.size() - 1)
981 cerr << "unexpected dummy size: " << dummy.size()
982 << " content: " << lines[row] << "\n";
985 lines[row] = dummy[1];
987 //cerr << "line: " << row << " above 0: " << dummy[0] << "\n";
988 //cerr << "line: " << row << " below 2: " << dummy[2] << "\n";
989 //cerr << "line: " << row << " cells 1: " << dummy[1] << "\n";
991 for (int i = 0; i <= 2; i += 2) {
992 //cerr << " reading from line string '" << dummy[i] << "'\n";
995 Token t = p1.get_token();
996 //cerr << "read token: " << t << "\n";
997 if (t.cs() == "hline" || t.cs() == "toprule" ||
998 t.cs() == "midrule" ||
999 t.cs() == "bottomrule") {
1000 if (t.cs() != "hline")
1003 if (rowinfo[row].topline) {
1004 if (row > 0) // extra bottomline above
1005 handle_hline_below(rowinfo[row - 1], cellinfo[row - 1]);
1007 cerr << "dropping extra "
1009 //cerr << "below row: " << row-1 << endl;
1011 handle_hline_above(rowinfo[row], cellinfo[row]);
1012 //cerr << "above row: " << row << endl;
1015 //cerr << "below row: " << row << endl;
1016 handle_hline_below(rowinfo[row], cellinfo[row]);
1018 } else if (t.cs() == "cline" || t.cs() == "cmidrule") {
1019 if (t.cs() == "cmidrule")
1021 string arg = p1.verbatim_item();
1022 //cerr << "read " << t.cs() << " arg: '" << arg << "'\n";
1023 vector<string> cols;
1024 split(arg, cols, '-');
1026 size_t from = convert<unsigned int>(cols[0]);
1028 cerr << "Could not parse "
1029 << t.cs() << " start column."
1032 // 1 based index -> 0 based
1034 if (from >= colinfo.size()) {
1035 cerr << t.cs() << " starts at "
1036 "non existing column "
1037 << (from + 1) << endl;
1038 from = colinfo.size() - 1;
1040 size_t to = convert<unsigned int>(cols[1]);
1042 cerr << "Could not parse "
1043 << t.cs() << " end column."
1046 // 1 based index -> 0 based
1048 if (to >= colinfo.size()) {
1049 cerr << t.cs() << " ends at "
1050 "non existing column "
1051 << (to + 1) << endl;
1052 to = colinfo.size() - 1;
1054 for (size_t col = from; col <= to; ++col) {
1055 //cerr << "row: " << row << " col: " << col << " i: " << i << endl;
1057 rowinfo[row].topline = true;
1058 cellinfo[row][col].topline = true;
1060 rowinfo[row].bottomline = true;
1061 cellinfo[row][col].bottomline = true;
1064 } else if (t.cs() == "addlinespace") {
1066 string const opt = p.next_token().cat() == catBegin ?
1067 p.verbatim_item() : string();
1070 rowinfo[row].top_space = "default";
1072 rowinfo[row].top_space = translate_len(opt);
1073 } else if (rowinfo[row].bottomline) {
1075 rowinfo[row].bottom_space = "default";
1077 rowinfo[row].bottom_space = translate_len(opt);
1080 rowinfo[row].interline_space = "default";
1082 rowinfo[row].interline_space = translate_len(opt);
1084 } else if (t.cs() == "endhead") {
1086 endhead.empty = true;
1088 rowinfo[row].type = LT_HEAD;
1089 for (int r = row - 1; r >= 0; --r) {
1090 if (rowinfo[r].type != LT_NORMAL)
1092 rowinfo[r].type = LT_HEAD;
1093 endhead.empty = false;
1096 } else if (t.cs() == "endfirsthead") {
1098 endfirsthead.empty = true;
1100 rowinfo[row].type = LT_FIRSTHEAD;
1101 for (int r = row - 1; r >= 0; --r) {
1102 if (rowinfo[r].type != LT_NORMAL)
1104 rowinfo[r].type = LT_FIRSTHEAD;
1105 endfirsthead.empty = false;
1107 endfirsthead.set = true;
1108 } else if (t.cs() == "endfoot") {
1110 endfoot.empty = true;
1112 rowinfo[row].type = LT_FOOT;
1113 for (int r = row - 1; r >= 0; --r) {
1114 if (rowinfo[r].type != LT_NORMAL)
1116 rowinfo[r].type = LT_FOOT;
1117 endfoot.empty = false;
1120 } else if (t.cs() == "endlastfoot") {
1122 endlastfoot.empty = true;
1124 rowinfo[row].type = LT_LASTFOOT;
1125 for (int r = row - 1; r >= 0; --r) {
1126 if (rowinfo[r].type != LT_NORMAL)
1128 rowinfo[r].type = LT_LASTFOOT;
1129 endlastfoot.empty = false;
1131 endlastfoot.set = true;
1132 } else if (t.cs() == "newpage") {
1135 rowinfo[row - 1].newpage = true;
1137 // This does not work in LaTeX
1143 rowinfo[row].newpage = true;
1145 cerr << "unexpected line token: " << t << endl;
1150 // LyX ends headers and footers always with \tabularnewline.
1151 // This causes one additional row in the output.
1152 // If the last row of a header/footer is empty, we can work
1153 // around that by removing it.
1155 RowInfo test = rowinfo[row-1];
1156 test.type = LT_NORMAL;
1157 if (lines[row-1].empty() && !test.special()) {
1158 switch (rowinfo[row-1].type) {
1160 if (rowinfo[row].type != LT_FIRSTHEAD &&
1161 rowinfo[row-2].type == LT_FIRSTHEAD)
1162 deletelastrow = true;
1165 if (rowinfo[row].type != LT_HEAD &&
1166 rowinfo[row-2].type == LT_HEAD)
1167 deletelastrow = true;
1170 if (rowinfo[row].type != LT_FOOT &&
1171 rowinfo[row-2].type == LT_FOOT)
1172 deletelastrow = true;
1175 if (rowinfo[row].type != LT_LASTFOOT &&
1176 rowinfo[row-2].type == LT_LASTFOOT)
1177 deletelastrow = true;
1185 if (deletelastrow) {
1186 lines.erase(lines.begin() + (row - 1));
1187 rowinfo.erase(rowinfo.begin() + (row - 1));
1188 cellinfo.erase(cellinfo.begin() + (row - 1));
1193 vector<string> cells;
1194 split(lines[row], cells, TAB);
1195 for (size_t col = 0, cell = 0; cell < cells.size();
1197 //cerr << "cell content: '" << cells[cell] << "'\n";
1198 if (col >= colinfo.size()) {
1199 // This does not work in LaTeX
1200 cerr << "Ignoring extra cell '"
1201 << cells[cell] << "'." << endl;
1204 string cellcont = cells[cell];
1205 // For decimal cells, ass the content of the second one to the first one
1207 if (colinfo[col].decimal_point != '\0' && colinfo[col].align == 'd' && cell < cells.size() - 1)
1208 cellcont += colinfo[col].decimal_point + cells[cell + 1];
1209 Parser parse(cellcont);
1210 parse.skip_spaces();
1211 //cells[cell] << "'\n";
1212 if (parse.next_token().cs() == "multirow") {
1213 // We do not support the vpos arg yet.
1214 if (parse.hasOpt()) {
1215 string const vpos = parse.getArg('[', ']');
1216 parse.skip_spaces(true);
1217 cerr << "Ignoring multirow's vpos arg '"
1218 << vpos << "'!" << endl;
1222 size_t const ncells =
1223 convert<unsigned int>(parse.verbatim_item());
1224 // We do not support the bigstrut arg yet.
1225 if (parse.hasOpt()) {
1226 string const bs = parse.getArg('[', ']');
1227 parse.skip_spaces(true);
1228 cerr << "Ignoring multirow's bigstrut arg '"
1229 << bs << "'!" << endl;
1231 // the width argument
1232 string const width = parse.getArg('{', '}');
1235 if (parse.hasOpt()) {
1236 vmove = parse.getArg('[', ']');
1237 parse.skip_spaces(true);
1241 colinfo[col].width = width;
1243 cellinfo[row][col].mroffset = vmove;
1244 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTIROW;
1245 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1246 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1247 cellinfo[row][col].mrxnum = ncells - 1;
1250 parse_text_in_inset(parse, os2, FLAG_ITEM, false, context);
1251 if (!cellinfo[row][col].content.empty()) {
1252 // This may or may not work in LaTeX,
1253 // but it does not work in LyX.
1254 // FIXME: Handle it correctly!
1255 cerr << "Moving cell content '"
1257 << "' into a multirow cell. "
1258 "This will probably not work."
1261 cellinfo[row][col].content += os2.str();
1262 } else if (parse.next_token().cs() == "multicolumn") {
1265 size_t const ncells =
1266 convert<unsigned int>(parse.verbatim_item());
1268 // special cell properties alignment
1270 handle_colalign(parse, t, ColInfo());
1271 parse.skip_spaces(true);
1272 ColInfo & ci = t.front();
1274 // The logic of LyX for multicolumn vertical
1275 // lines is too complicated to reproduce it
1276 // here (see LyXTabular::TeXCellPreamble()).
1277 // Therefore we simply put everything in the
1281 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1282 cellinfo[row][col].align = ci.align;
1283 cellinfo[row][col].special = ci.special;
1284 cellinfo[row][col].leftlines = ci.leftlines;
1285 cellinfo[row][col].rightlines = ci.rightlines;
1287 parse_text_in_inset(parse, os2, FLAG_ITEM, false, context);
1288 if (!cellinfo[row][col].content.empty()) {
1289 // This may or may not work in LaTeX,
1290 // but it does not work in LyX.
1291 // FIXME: Handle it correctly!
1292 cerr << "Moving cell content '"
1294 << "' into a multicolumn cell. "
1295 "This will probably not work."
1298 cellinfo[row][col].content += os2.str();
1300 // add dummy cells for multicol
1301 for (size_t i = 0; i < ncells - 1; ++i) {
1303 if (col >= colinfo.size()) {
1304 cerr << "The cell '"
1307 << convert<string>(ncells)
1308 << " columns while the table has only "
1309 << convert<string>(colinfo.size())
1311 << " Therefore the surplus columns will be ignored."
1315 cellinfo[row][col].multi = CELL_PART_OF_MULTICOLUMN;
1316 cellinfo[row][col].align = 'c';
1319 } else if (col == 0 && colinfo.size() > 1 &&
1321 parse.next_token().cs() == "caption") {
1322 // longtable caption support in LyX is a hack:
1323 // Captions require a row of their own with
1324 // the caption flag set to true, having only
1325 // one multicolumn cell. The contents of that
1326 // cell must contain exactly one caption inset
1327 // and nothing else.
1328 // Fortunately, the caption flag is only needed
1329 // for tables with more than one column.
1330 rowinfo[row].caption = true;
1331 for (size_t c = 1; c < cells.size(); ++c) {
1332 if (!cells[c].empty()) {
1333 cerr << "Moving cell content '"
1335 << "' into the caption cell. "
1336 "This will probably not work."
1338 cells[0] += cells[c];
1342 cellinfo[row][col].align = colinfo[col].align;
1343 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1345 parse_text_in_inset(parse, os2, FLAG_CELL, false, context);
1346 cellinfo[row][col].content += os2.str();
1347 // add dummy multicolumn cells
1348 for (size_t c = 1; c < colinfo.size(); ++c)
1349 cellinfo[row][c].multi = CELL_PART_OF_MULTICOLUMN;
1353 if (parse.next_token().cs() == "begin") {
1354 parse.pushPosition();
1356 string const env = parse.getArg('{', '}');
1357 if (env == "sideways" || env == "turn") {
1358 string angle = "90";
1359 if (env == "turn") {
1361 angle = parse.getArg('{', '}');
1363 active_environments.push_back(env);
1364 parse.ertEnvironment(env);
1365 active_environments.pop_back();
1366 parse.skip_spaces();
1367 if (!parse.good() && support::isStrInt(angle))
1368 rotate = convert<int>(angle);
1370 parse.popPosition();
1372 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1373 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1374 cellinfo[row][col].align = colinfo[col].align;
1377 cellinfo[row][col].rotate = rotate;
1379 active_environments.push_back(parse.getArg('{', '}'));
1381 parse.getArg('{', '}');
1382 parse_text_in_inset(parse, os2, FLAG_END, false, context);
1383 active_environments.pop_back();
1384 preamble.registerAutomaticallyLoadedPackage("rotating");
1386 parse_text_in_inset(parse, os2, FLAG_CELL, false, context);
1388 cellinfo[row][col].content += os2.str();
1392 //cerr << "// handle almost empty last row what we have\n";
1393 // handle almost empty last row
1394 if (row && lines[row].empty() && row + 1 == rowinfo.size()) {
1395 //cerr << "remove empty last line\n";
1396 if (rowinfo[row].topline)
1397 rowinfo[row - 1].bottomline = true;
1398 for (size_t col = 0; col < colinfo.size(); ++col)
1399 if (cellinfo[row][col].topline)
1400 cellinfo[row - 1][col].bottomline = true;
1407 // Now we have the table structure and content in rowinfo, colinfo
1409 // Unfortunately LyX has some limitations that we need to work around.
1412 for (size_t row = 0; row < rowinfo.size(); ++row) {
1413 for (size_t col = 0; col < cellinfo[row].size(); ++col) {
1414 // Convert cells with special content to multicolumn cells
1415 // (LyX ignores the special field for non-multicolumn cells).
1416 if (cellinfo[row][col].multi == CELL_NORMAL &&
1417 !cellinfo[row][col].special.empty())
1418 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1419 // Add multirow dummy cells
1420 if (row > 1 && (cellinfo[row - 1][col].multi == CELL_PART_OF_MULTIROW
1421 || cellinfo[row - 1][col].multi == CELL_BEGIN_OF_MULTIROW)
1422 && cellinfo[row - 1][col].mrxnum > 0) {
1423 // add dummy cells for multirow
1424 cellinfo[row][col].multi = CELL_PART_OF_MULTIROW;
1425 cellinfo[row][col].align = 'c';
1426 cellinfo[row][col].mrxnum = cellinfo[row - 1][col].mrxnum - 1;
1431 // Distribute lines from rows/columns to cells
1432 // The code was stolen from convert_tablines() in lyx2lyx/lyx_1_6.py.
1433 // Each standard cell inherits the settings of the corresponding
1434 // rowinfo/colinfo. This works because all cells with individual
1435 // settings were converted to multicolumn cells above.
1436 // Each multicolumn cell inherits the settings of the rowinfo/colinfo
1437 // corresponding to the first column of the multicolumn cell (default
1438 // of the multicol package). This works because the special field
1439 // overrides the line fields.
1440 for (size_t row = 0; row < rowinfo.size(); ++row) {
1441 for (size_t col = 0; col < cellinfo[row].size(); ++col) {
1442 if (cellinfo[row][col].multi == CELL_NORMAL) {
1443 cellinfo[row][col].topline = rowinfo[row].topline;
1444 cellinfo[row][col].bottomline = rowinfo[row].bottomline;
1445 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1446 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1447 } else if (cellinfo[row][col].multi == CELL_BEGIN_OF_MULTICOLUMN) {
1449 while (s < cellinfo[row].size() &&
1450 cellinfo[row][s].multi == CELL_PART_OF_MULTICOLUMN)
1452 if (s < cellinfo[row].size() &&
1453 cellinfo[row][s].multi != CELL_BEGIN_OF_MULTICOLUMN)
1454 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1455 if (col > 0 && cellinfo[row][col-1].multi == CELL_NORMAL)
1456 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1457 } else if (cellinfo[row][col].multi == CELL_BEGIN_OF_MULTIROW) {
1459 while (s < rowinfo.size() &&
1460 cellinfo[s][col].multi == CELL_PART_OF_MULTIROW)
1462 if (s < cellinfo[row].size() &&
1463 cellinfo[s][col].multi != CELL_BEGIN_OF_MULTIROW)
1464 cellinfo[row][col].bottomline = rowinfo[row].bottomline;
1465 if (row > 0 && cellinfo[row - 1][col].multi == CELL_NORMAL)
1466 cellinfo[row][col].topline = rowinfo[row].topline;
1472 preamble.registerAutomaticallyLoadedPackage("booktabs");
1473 if (name == "longtable")
1474 preamble.registerAutomaticallyLoadedPackage("longtable");
1475 else if (name == "xltabular")
1476 preamble.registerAutomaticallyLoadedPackage("xltabular");
1477 else if (name == "tabularx")
1478 preamble.registerAutomaticallyLoadedPackage("tabularx");
1480 //cerr << "// output what we have\n";
1481 // output what we have
1482 size_type cols = colinfo.size();
1483 for (size_t col = 0; col < colinfo.size(); ++col) {
1484 if (colinfo[col].decimal_point != '\0' && colinfo[col].align != 'd')
1487 os << "\n<lyxtabular version=\"3\" rows=\"" << rowinfo.size()
1488 << "\" columns=\"" << cols << "\">\n";
1490 << write_attribute("rotate", context.tablerotation)
1491 << write_attribute("booktabs", booktabs)
1492 << write_attribute("islongtable", is_long_tabular);
1493 if (is_long_tabular) {
1494 os << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1495 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1496 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1497 << write_attribute("headTopDL", endhead.topDL)
1498 << write_attribute("headBottomDL", endhead.bottomDL)
1499 << write_attribute("footTopDL", endfoot.topDL)
1500 << write_attribute("footBottomDL", endfoot.bottomDL)
1501 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1502 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1503 << write_attribute("lastFootEmpty", endlastfoot.empty);
1504 if (!halign.empty())
1505 os << write_attribute("longtabularalignment", halign);
1507 os << write_attribute("tabularvalignment", tabularvalignment);
1509 os << write_attribute("tabularwidth", tabularwidth) << ">\n";
1511 //cerr << "// after header\n";
1512 for (size_t col = 0; col < colinfo.size(); ++col) {
1513 if (colinfo[col].decimal_point != '\0' && colinfo[col].align != 'd')
1515 os << "<column alignment=\""
1516 << verbose_align(colinfo[col].align) << "\"";
1517 if (colinfo[col].decimal_point != '\0')
1518 os << " decimal_point=\"" << colinfo[col].decimal_point << "\"";
1519 os << " valignment=\""
1520 << verbose_valign(colinfo[col].valign) << "\""
1521 << write_attribute("width", translate_len(colinfo[col].width))
1522 << write_attribute("special", colinfo[col].special)
1523 << write_attribute("varwidth", colinfo[col].varwidth)
1526 //cerr << "// after cols\n";
1528 for (size_t row = 0; row < rowinfo.size(); ++row) {
1530 << write_attribute("topspace", rowinfo[row].top_space)
1531 << write_attribute("bottomspace", rowinfo[row].bottom_space)
1532 << write_attribute("interlinespace", rowinfo[row].interline_space)
1533 << write_attribute("endhead",
1534 rowinfo[row].type == LT_HEAD)
1535 << write_attribute("endfirsthead",
1536 rowinfo[row].type == LT_FIRSTHEAD)
1537 << write_attribute("endfoot",
1538 rowinfo[row].type == LT_FOOT)
1539 << write_attribute("endlastfoot",
1540 rowinfo[row].type == LT_LASTFOOT)
1541 << write_attribute("newpage", rowinfo[row].newpage)
1542 << write_attribute("caption", rowinfo[row].caption)
1544 for (size_t col = 0; col < colinfo.size(); ++col) {
1545 CellInfo const & cell = cellinfo[row][col];
1546 if (colinfo[col].decimal_point != '\0' && colinfo[col].align != 'd')
1547 // These are the second columns in a salign pair. Skip.
1550 if ((cell.multi == CELL_BEGIN_OF_MULTICOLUMN
1551 || cell.multi == CELL_PART_OF_MULTICOLUMN)
1552 && colinfo[col].align != 'd')
1553 os << " multicolumn=\"" << cell.multi << "\"";
1554 if (cell.multi == CELL_BEGIN_OF_MULTIROW
1555 || cell.multi == CELL_PART_OF_MULTIROW)
1556 os << " multirow=\"" << cell.multi << "\"";
1557 os << " alignment=\"" << verbose_align(cell.align)
1559 << " valignment=\"" << verbose_valign(cell.valign)
1561 << write_attribute("topline", cell.topline)
1562 << write_attribute("bottomline", cell.bottomline)
1563 << write_attribute("leftline", cell.leftlines > 0)
1564 << write_attribute("rightline", cell.rightlines > 0)
1565 << write_attribute("rotate", cell.rotate)
1566 << write_attribute("mroffset", cell.mroffset);
1567 //cerr << "\nrow: " << row << " col: " << col;
1569 // cerr << " topline=\"true\"";
1570 //if (cell.bottomline)
1571 // cerr << " bottomline=\"true\"";
1572 os << " usebox=\"none\""
1573 << write_attribute("width", translate_len(cell.width));
1574 if (cell.multi != CELL_NORMAL)
1575 os << write_attribute("special", cell.special);
1577 << "\n\\begin_inset Text\n"
1579 << "\n\\end_inset\n"
1585 os << "</lyxtabular>\n";