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,
112 /// This is a dummy cell (part of a multicolumn cell)
113 CELL_PART_OF_MULTICOLUMN,
115 CELL_BEGIN_OF_MULTIROW,
117 CELL_PART_OF_MULTIROW
123 CellInfo() : multi(CELL_NORMAL), align('n'), valign('n'),
124 leftlines(0), rightlines(0), topline(false),
125 bottomline(false), rotate(0), mrxnum(0) {}
132 /// vertical cell alignment
134 /// number of lines on the left
136 /// number of lines on the right
138 /// do we have a line above?
140 /// do we have a line below?
142 /// how is the cell rotated?
144 /// width for multicolumn cells
146 /// special formatting for multicolumn cells
150 /// number of further multirows
158 ltType() : set(false), topDL(false), bottomDL(false), empty(false) {}
159 // we have this header type (is set in the getLT... functions)
161 // double borders on top
163 // double borders on bottom
165 // used for FirstHeader & LastFooter and if this is true
166 // all the rows marked as FirstHeader or LastFooter are
167 // ignored in the output and it is set to be empty!
172 /// translate a horizontal alignment (as stored in ColInfo and CellInfo) to LyX
173 inline char const * verbose_align(char c)
188 /// translate a vertical alignment (as stored in ColInfo and CellInfo) to LyX
189 inline char const * verbose_valign(char c)
191 // The default value for no special alignment is "top".
204 // stripped down from tabluar.C. We use it currently only for bools and
206 string const write_attribute(string const & name, bool const & b)
208 // we write only true attribute values so we remove a bit of the
209 // file format bloat for tabulars.
210 return b ? ' ' + name + "=\"true\"" : string();
214 string const write_attribute(string const & name, string const & s)
216 return s.empty() ? string() : ' ' + name + "=\"" + s + '"';
220 string const write_attribute(string const & name, int const & i)
222 // we write only true attribute values so we remove a bit of the
223 // file format bloat for tabulars.
224 return i ? write_attribute(name, convert<string>(i)) : string();
228 /*! rather brutish way to code table structure in a string:
233 \multicolumn{2}{c}{4} & 5 //
239 gets "translated" to:
242 HLINE 1 TAB 2 TAB 3 HLINE HLINE LINE
243 \hline HLINE \multicolumn{2}{c}{4} TAB 5 HLINE HLINE LINE
244 HLINE 6 TAB 7 HLINE HLINE LINE
245 HLINE 8 HLINE \endhead HLINE LINE
249 char const TAB = '\001';
250 char const LINE = '\002';
251 char const HLINE = '\004';
255 * Move the information in leftlines, rightlines, align and valign to the
256 * special field. This is necessary if the special field is not empty,
257 * because LyX ignores leftlines > 1, rightlines > 1, align and valign in
260 void ci2special(ColInfo & ci)
262 if (ci.width.empty() && ci.align == 'n')
263 // The alignment setting is already in special, since
264 // handle_colalign() never stores ci with these settings
265 // and ensures that leftlines == 0 and rightlines == 0 in
269 if (!ci.width.empty()) {
270 string arraybackslash;
272 arraybackslash = "\\arraybackslash";
275 ci.special += ">{\\raggedright" + arraybackslash + "}";
278 ci.special += ">{\\raggedleft" + arraybackslash + "}";
281 ci.special += ">{\\centering" + arraybackslash + "}";
286 else if (ci.valign == 'n')
289 ci.special += ci.valign;
290 ci.special += '{' + ci.width + '}';
293 ci.special += ci.align;
295 // LyX can only have one left and one right line.
296 for (int i = 1; i < ci.leftlines; ++i)
297 ci.special.insert(0, "|");
298 for (int i = 1; i < ci.rightlines; ++i)
300 ci.leftlines = min(ci.leftlines, 1);
301 ci.rightlines = min(ci.rightlines, 1);
308 * Handle column specifications for tabulars and multicolumns.
309 * The next token of the parser \p p must be an opening brace, and we read
310 * everything until the matching closing brace.
311 * The resulting column specifications are filled into \p colinfo. This is
312 * in an intermediate form. fix_colalign() makes it suitable for LyX output.
314 void handle_colalign(Parser & p, vector<ColInfo> & colinfo,
315 ColInfo const & start)
317 if (p.get_token().cat() != catBegin)
318 cerr << "Wrong syntax for table column alignment.\n"
319 "Expected '{', got '" << p.curr_token().asInput()
322 ColInfo next = start;
323 for (Token t = p.get_token(); p.good() && t.cat() != catEnd;
326 cerr << "t: " << t << " c: '" << t.character() << "'\n";
329 // We cannot handle comments here
330 if (t.cat() == catComment) {
331 if (t.cs().empty()) {
335 cerr << "Ignoring comment: " << t.asInput();
339 switch (t.character()) {
341 // whitespace, ignore.
346 // new column, horizontal aligned
347 next.align = t.character();
348 if (!next.special.empty())
350 colinfo.push_back(next);
355 next.varwidth = true;
356 if (!next.special.empty())
358 colinfo.push_back(next);
364 // new column, vertical aligned box
365 next.valign = t.character();
366 next.width = p.verbatim_item();
367 if (!next.special.empty())
369 colinfo.push_back(next);
374 if (colinfo.empty()) {
375 if (next.special.empty())
379 } else if (colinfo.back().special.empty())
380 ++colinfo.back().rightlines;
381 else if (next.special.empty())
384 colinfo.back().special += '|';
387 // text before the next column
388 string const s = trimSpaceAndEol(p.verbatim_item());
389 if (next.special.empty() &&
391 // Maybe this can be converted to a
392 // horizontal alignment setting for
393 // fixed width columns
394 if (s == "\\raggedleft" || s == "\\raggedleft\\arraybackslash")
396 else if (s == "\\raggedright" || s == "\\raggedright\\arraybackslash")
398 else if (s == "\\centering" || s == "\\centering\\arraybackslash")
401 next.special = ">{" + s + '}';
403 next.special += ">{" + s + '}';
407 // text after the last column
408 string const s = trimSpaceAndEol(p.verbatim_item());
410 // This is not possible in LaTeX.
411 cerr << "Ignoring separator '<{"
412 << s << "}'." << endl;
414 ColInfo & ci = colinfo.back();
416 ci.special += "<{" + s + '}';
421 if (p.next_token().character() != '{')
423 // *{n}{arg} means 'n' columns of type 'arg'
424 string const num = p.verbatim_item();
425 string const arg = p.verbatim_item();
426 size_t const n = convert<unsigned int>(num);
427 if (!arg.empty() && n > 0) {
429 for (size_t i = 0; i < n; ++i)
433 handle_colalign(p2, colinfo, next);
436 cerr << "Ignoring column specification"
437 " '*{" << num << "}{"
438 << arg << "}'." << endl;
443 // text instead of the column spacing
445 // text in addition to the column spacing
446 next.special += t.character();
447 next.special += '{' + p.verbatim_item() + '}';
450 // try user defined column types
451 // unknown column types (nargs == -1) are
452 // assumed to consume no arguments
454 next.special += t.character();
456 preamble.getSpecialTableColumnArguments(t.character());
457 for (int i = 0; i < nargs; ++i)
458 next.special += '{' +
459 p.verbatim_item() + '}';
460 colinfo.push_back(next);
467 // Maybe we have some column separators that need to be added to the
470 if (!next.special.empty()) {
471 ColInfo & ci = colinfo.back();
473 ci.special += next.special;
474 next.special.erase();
480 * Move the left and right lines and alignment settings of the column \p ci
481 * to the special field if necessary.
483 void fix_colalign(ColInfo & ci)
485 if (ci.leftlines > 1 || ci.rightlines > 1)
491 * LyX can't handle more than one vertical line at the left or right side
493 * This function moves the left and right lines and alignment settings of all
494 * columns in \p colinfo to the special field if necessary.
496 void fix_colalign(vector<ColInfo> & colinfo)
498 // Try to move extra leftlines to the previous column.
499 // We do this only if both special fields are empty, otherwise we
500 // can't tell wether the result will be the same.
501 for (size_t col = 0; col < colinfo.size(); ++col) {
502 if (colinfo[col].leftlines > 1 &&
503 colinfo[col].special.empty() && col > 0 &&
504 colinfo[col - 1].rightlines == 0 &&
505 colinfo[col - 1].special.empty()) {
506 ++colinfo[col - 1].rightlines;
507 --colinfo[col].leftlines;
510 // Try to move extra rightlines to the next column
511 for (size_t col = 0; col < colinfo.size(); ++col) {
512 if (colinfo[col].rightlines > 1 &&
513 colinfo[col].special.empty() &&
514 col < colinfo.size() - 1 &&
515 colinfo[col + 1].leftlines == 0 &&
516 colinfo[col + 1].special.empty()) {
517 ++colinfo[col + 1].leftlines;
518 --colinfo[col].rightlines;
521 // Move the lines and alignment settings to the special field if
523 for (size_t col = 0; col < colinfo.size(); ++col)
524 fix_colalign(colinfo[col]);
529 * Parse hlines and similar stuff.
530 * \returns wether the token \p t was parsed
532 bool parse_hlines(Parser & p, Token const & t, string & hlines,
533 bool is_long_tabular)
535 LASSERT(t.cat() == catEscape, return false);
537 if (t.cs() == "hline" || t.cs() == "toprule" || t.cs() == "midrule" ||
538 t.cs() == "bottomrule")
539 hlines += '\\' + t.cs();
541 else if (t.cs() == "cline")
542 hlines += "\\cline{" + p.verbatim_item() + '}';
544 else if (t.cs() == "cmidrule") {
545 // We cannot handle the \cmidrule(l){3-4} form
548 bool const hasParentheses(p.getFullArg('(', ')').first);
552 hlines += "\\cmidrule{" + p.verbatim_item() + '}';
555 else if (t.cs() == "addlinespace") {
558 bool const hasArgument(p.getFullArg('{', '}').first);
561 hlines += "\\addlinespace{" + p.verbatim_item() + '}';
563 hlines += "\\addlinespace";
566 else if (is_long_tabular && t.cs() == "newpage")
567 hlines += "\\newpage";
576 /// Position in a row
578 /// At the very beginning, before the first token
580 /// After the first token and before any column token
582 /// After the first column token. Comments and whitespace are only
583 /// treated as tokens in this position
585 /// After the first non-column token at the end
591 * Parse table structure.
592 * We parse tables in a two-pass process: This function extracts the table
593 * structure (rows, columns, hlines etc.), but does not change the cell
594 * content. The cell content is parsed in a second step in handle_tabular().
596 void parse_table(Parser & p, ostream & os, bool is_long_tabular,
597 RowPosition & pos, unsigned flags)
599 // table structure commands such as \hline
602 // comments that occur at places where we can't handle them
606 Token const & t = p.get_token();
609 debugToken(cerr, t, flags);
612 // comments and whitespace in hlines
615 case IN_HLINES_START:
617 if (t.cat() == catComment) {
622 // We can't handle comments here,
623 // store them for later use
624 comments += t.asInput();
626 } else if (t.cat() == catSpace ||
627 t.cat() == catNewline) {
628 // whitespace is irrelevant here, we
629 // need to recognize hline stuff
638 // We need to handle structure stuff first in order to
639 // determine wether we need to output a HLINE separator
640 // before the row or not.
641 if (t.cat() == catEscape) {
642 if (parse_hlines(p, t, hlines, is_long_tabular)) {
645 pos = IN_HLINES_START;
650 case IN_HLINES_START:
657 else if (t.cs() == "tabularnewline" ||
661 cerr << "Warning: Converting TeX "
662 "'\\cr' to LaTeX '\\\\'."
664 // stuff before the line break
665 os << comments << HLINE << hlines << HLINE
667 //cerr << "hlines: " << hlines << endl;
674 else if (is_long_tabular &&
675 (t.cs() == "endhead" ||
676 t.cs() == "endfirsthead" ||
677 t.cs() == "endfoot" ||
678 t.cs() == "endlastfoot")) {
679 hlines += t.asInput();
683 // these commands are implicit line
685 os << comments << HLINE << hlines
692 pos = IN_HLINES_START;
694 case IN_HLINES_START:
701 // We need a HLINE separator if we either have no hline
702 // stuff at all and are just starting a row or if we just
703 // got the first non-hline token.
706 // no hline tokens exist, first token at row start
707 case IN_HLINES_START:
708 // hline tokens exist, first non-hline token at row
710 os << hlines << HLINE << comments;
716 // Oops, there is still cell content or unsupported
717 // booktabs commands after hline stuff. The latter are
718 // moved to the cell, and the first does not work in
719 // LaTeX, so we ignore the hlines.
722 if (support::contains(hlines, "\\hline") ||
723 support::contains(hlines, "\\cline") ||
724 support::contains(hlines, "\\newpage"))
725 cerr << "Ignoring '" << hlines
726 << "' in a cell" << endl;
736 // If we come here we have normal cell content
740 if (t.cat() == catMath) {
741 // we are inside some text mode thingy, so opening new math is allowed
742 Token const & n = p.get_token();
743 if (n.cat() == catMath) {
744 // TeX's $$...$$ syntax for displayed math
746 // This does only work because parse_math outputs TeX
747 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
749 p.get_token(); // skip the second '$' token
751 // simple $...$ stuff
754 // This does only work because parse_math outputs TeX
755 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
760 else if (t.cat() == catSpace
761 || t.cat() == catNewline
762 || t.cat() == catLetter
763 || t.cat() == catSuper
765 || t.cat() == catOther
766 || t.cat() == catActive
767 || t.cat() == catParameter)
770 else if (t.cat() == catBegin) {
772 parse_table(p, os, is_long_tabular, pos,
777 else if (t.cat() == catEnd) {
778 if (flags & FLAG_BRACE_LAST)
780 cerr << "unexpected '}'\n";
783 else if (t.cat() == catAlign) {
788 else if (t.cat() == catComment)
791 else if (t.cs() == "(") {
793 // This does only work because parse_math outputs TeX
794 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
798 else if (t.cs() == "[") {
800 // This does only work because parse_math outputs TeX
801 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
805 else if (t.cs() == "begin") {
806 string const name = p.getArg('{', '}');
807 active_environments.push_back(name);
808 os << "\\begin{" << name << '}';
809 // treat the nested environment as a block, don't
810 // parse &, \\ etc, because they don't belong to our
811 // table if they appear.
812 os << p.ertEnvironment(name);
813 os << "\\end{" << name << '}';
814 active_environments.pop_back();
817 else if (t.cs() == "end") {
818 if (flags & FLAG_END) {
819 // eat environment name
820 string const name = p.getArg('{', '}');
821 if (name != active_environment())
822 p.error("\\end{" + name + "} does not match \\begin{"
823 + active_environment() + "}");
826 p.error("found 'end' unexpectedly");
833 // We can have comments if the last line is incomplete
836 // We can have hline stuff if the last line is incomplete
837 if (!hlines.empty()) {
838 // this does not work in LaTeX, so we ignore it
839 cerr << "Ignoring '" << hlines << "' at end of tabular"
845 void handle_hline_above(RowInfo & ri, vector<CellInfo> & ci)
848 for (size_t col = 0; col < ci.size(); ++col)
849 ci[col].topline = true;
853 void handle_hline_below(RowInfo & ri, vector<CellInfo> & ci)
855 ri.bottomline = true;
856 for (size_t col = 0; col < ci.size(); ++col)
857 ci[col].bottomline = true;
861 } // anonymous namespace
864 void handle_tabular(Parser & p, ostream & os, string const & name,
865 string const & tabularwidth, string const & halign,
868 bool const is_long_tabular(name == "longtable" || name == "xltabular");
869 bool booktabs = false;
870 string tabularvalignment("middle");
871 string posopts = p.getOpt();
872 if (!posopts.empty()) {
873 if (posopts == "[t]")
874 tabularvalignment = "top";
875 else if (posopts == "[b]")
876 tabularvalignment = "bottom";
878 cerr << "vertical tabular positioning '"
879 << posopts << "' ignored\n";
882 vector<ColInfo> colinfo;
884 // handle column formatting
885 handle_colalign(p, colinfo, ColInfo());
886 fix_colalign(colinfo);
888 // first scan of cells
889 // use table mode to keep it minimal-invasive
890 // not exactly what's TeX doing...
891 vector<string> lines;
893 RowPosition rowpos = ROW_START;
894 parse_table(p, ss, is_long_tabular, rowpos, FLAG_END);
895 split(ss.str(), lines, LINE);
897 vector< vector<CellInfo> > cellinfo(lines.size());
898 vector<RowInfo> rowinfo(lines.size());
905 //cerr << "// split into rows\n";
906 for (size_t row = 0; row < rowinfo.size();) {
909 cellinfo[row].resize(colinfo.size());
910 bool deletelastrow = false;
913 vector<string> dummy;
914 //cerr << "\n########### LINE: " << lines[row] << "########\n";
915 split(lines[row], dummy, HLINE);
917 // handle horizontal line fragments
918 // we do only expect this for a last line without '\\'
919 if (dummy.size() != 3) {
920 if ((dummy.size() != 1 && dummy.size() != 2) ||
921 row != rowinfo.size() - 1)
922 cerr << "unexpected dummy size: " << dummy.size()
923 << " content: " << lines[row] << "\n";
926 lines[row] = dummy[1];
928 //cerr << "line: " << row << " above 0: " << dummy[0] << "\n";
929 //cerr << "line: " << row << " below 2: " << dummy[2] << "\n";
930 //cerr << "line: " << row << " cells 1: " << dummy[1] << "\n";
932 for (int i = 0; i <= 2; i += 2) {
933 //cerr << " reading from line string '" << dummy[i] << "'\n";
936 Token t = p1.get_token();
937 //cerr << "read token: " << t << "\n";
938 if (t.cs() == "hline" || t.cs() == "toprule" ||
939 t.cs() == "midrule" ||
940 t.cs() == "bottomrule") {
941 if (t.cs() != "hline")
944 if (rowinfo[row].topline) {
945 if (row > 0) // extra bottomline above
946 handle_hline_below(rowinfo[row - 1], cellinfo[row - 1]);
948 cerr << "dropping extra "
950 //cerr << "below row: " << row-1 << endl;
952 handle_hline_above(rowinfo[row], cellinfo[row]);
953 //cerr << "above row: " << row << endl;
956 //cerr << "below row: " << row << endl;
957 handle_hline_below(rowinfo[row], cellinfo[row]);
959 } else if (t.cs() == "cline" || t.cs() == "cmidrule") {
960 if (t.cs() == "cmidrule")
962 string arg = p1.verbatim_item();
963 //cerr << "read " << t.cs() << " arg: '" << arg << "'\n";
965 split(arg, cols, '-');
967 size_t from = convert<unsigned int>(cols[0]);
969 cerr << "Could not parse "
970 << t.cs() << " start column."
973 // 1 based index -> 0 based
975 if (from >= colinfo.size()) {
976 cerr << t.cs() << " starts at "
977 "non existing column "
978 << (from + 1) << endl;
979 from = colinfo.size() - 1;
981 size_t to = convert<unsigned int>(cols[1]);
983 cerr << "Could not parse "
984 << t.cs() << " end column."
987 // 1 based index -> 0 based
989 if (to >= colinfo.size()) {
990 cerr << t.cs() << " ends at "
991 "non existing column "
993 to = colinfo.size() - 1;
995 for (size_t col = from; col <= to; ++col) {
996 //cerr << "row: " << row << " col: " << col << " i: " << i << endl;
998 rowinfo[row].topline = true;
999 cellinfo[row][col].topline = true;
1001 rowinfo[row].bottomline = true;
1002 cellinfo[row][col].bottomline = true;
1005 } else if (t.cs() == "addlinespace") {
1007 string const opt = p.next_token().cat() == catBegin ?
1008 p.verbatim_item() : string();
1011 rowinfo[row].top_space = "default";
1013 rowinfo[row].top_space = translate_len(opt);
1014 } else if (rowinfo[row].bottomline) {
1016 rowinfo[row].bottom_space = "default";
1018 rowinfo[row].bottom_space = translate_len(opt);
1021 rowinfo[row].interline_space = "default";
1023 rowinfo[row].interline_space = translate_len(opt);
1025 } else if (t.cs() == "endhead") {
1027 endhead.empty = true;
1029 rowinfo[row].type = LT_HEAD;
1030 for (int r = row - 1; r >= 0; --r) {
1031 if (rowinfo[r].type != LT_NORMAL)
1033 rowinfo[r].type = LT_HEAD;
1034 endhead.empty = false;
1037 } else if (t.cs() == "endfirsthead") {
1039 endfirsthead.empty = true;
1041 rowinfo[row].type = LT_FIRSTHEAD;
1042 for (int r = row - 1; r >= 0; --r) {
1043 if (rowinfo[r].type != LT_NORMAL)
1045 rowinfo[r].type = LT_FIRSTHEAD;
1046 endfirsthead.empty = false;
1048 endfirsthead.set = true;
1049 } else if (t.cs() == "endfoot") {
1051 endfoot.empty = true;
1053 rowinfo[row].type = LT_FOOT;
1054 for (int r = row - 1; r >= 0; --r) {
1055 if (rowinfo[r].type != LT_NORMAL)
1057 rowinfo[r].type = LT_FOOT;
1058 endfoot.empty = false;
1061 } else if (t.cs() == "endlastfoot") {
1063 endlastfoot.empty = true;
1065 rowinfo[row].type = LT_LASTFOOT;
1066 for (int r = row - 1; r >= 0; --r) {
1067 if (rowinfo[r].type != LT_NORMAL)
1069 rowinfo[r].type = LT_LASTFOOT;
1070 endlastfoot.empty = false;
1072 endlastfoot.set = true;
1073 } else if (t.cs() == "newpage") {
1076 rowinfo[row - 1].newpage = true;
1078 // This does not work in LaTeX
1084 rowinfo[row].newpage = true;
1086 cerr << "unexpected line token: " << t << endl;
1091 // LyX ends headers and footers always with \tabularnewline.
1092 // This causes one additional row in the output.
1093 // If the last row of a header/footer is empty, we can work
1094 // around that by removing it.
1096 RowInfo test = rowinfo[row-1];
1097 test.type = LT_NORMAL;
1098 if (lines[row-1].empty() && !test.special()) {
1099 switch (rowinfo[row-1].type) {
1101 if (rowinfo[row].type != LT_FIRSTHEAD &&
1102 rowinfo[row-2].type == LT_FIRSTHEAD)
1103 deletelastrow = true;
1106 if (rowinfo[row].type != LT_HEAD &&
1107 rowinfo[row-2].type == LT_HEAD)
1108 deletelastrow = true;
1111 if (rowinfo[row].type != LT_FOOT &&
1112 rowinfo[row-2].type == LT_FOOT)
1113 deletelastrow = true;
1116 if (rowinfo[row].type != LT_LASTFOOT &&
1117 rowinfo[row-2].type == LT_LASTFOOT)
1118 deletelastrow = true;
1126 if (deletelastrow) {
1127 lines.erase(lines.begin() + (row - 1));
1128 rowinfo.erase(rowinfo.begin() + (row - 1));
1129 cellinfo.erase(cellinfo.begin() + (row - 1));
1134 vector<string> cells;
1135 split(lines[row], cells, TAB);
1136 for (size_t col = 0, cell = 0; cell < cells.size();
1138 //cerr << "cell content: '" << cells[cell] << "'\n";
1139 if (col >= colinfo.size()) {
1140 // This does not work in LaTeX
1141 cerr << "Ignoring extra cell '"
1142 << cells[cell] << "'." << endl;
1145 Parser parse(cells[cell]);
1146 parse.skip_spaces();
1147 //cells[cell] << "'\n";
1148 if (parse.next_token().cs() == "multirow") {
1149 // We do not support the vpos arg yet.
1150 if (parse.hasOpt()) {
1151 string const vpos = parse.getArg('[', ']');
1152 parse.skip_spaces(true);
1153 cerr << "Ignoring multirow's vpos arg '"
1154 << vpos << "'!" << endl;
1158 size_t const ncells =
1159 convert<unsigned int>(parse.verbatim_item());
1160 // We do not support the bigstrut arg yet.
1161 if (parse.hasOpt()) {
1162 string const bs = parse.getArg('[', ']');
1163 parse.skip_spaces(true);
1164 cerr << "Ignoring multirow's bigstrut arg '"
1165 << bs << "'!" << endl;
1167 // the width argument
1168 string const width = parse.getArg('{', '}');
1171 if (parse.hasOpt()) {
1172 vmove = parse.getArg('[', ']');
1173 parse.skip_spaces(true);
1177 colinfo[col].width = width;
1179 cellinfo[row][col].mroffset = vmove;
1180 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTIROW;
1181 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1182 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1183 cellinfo[row][col].mrxnum = ncells - 1;
1186 parse_text_in_inset(parse, os2, FLAG_ITEM, false, context);
1187 if (!cellinfo[row][col].content.empty()) {
1188 // This may or may not work in LaTeX,
1189 // but it does not work in LyX.
1190 // FIXME: Handle it correctly!
1191 cerr << "Moving cell content '"
1193 << "' into a multirow cell. "
1194 "This will probably not work."
1197 cellinfo[row][col].content += os2.str();
1198 } else if (parse.next_token().cs() == "multicolumn") {
1201 size_t const ncells =
1202 convert<unsigned int>(parse.verbatim_item());
1204 // special cell properties alignment
1206 handle_colalign(parse, t, ColInfo());
1207 parse.skip_spaces(true);
1208 ColInfo & ci = t.front();
1210 // The logic of LyX for multicolumn vertical
1211 // lines is too complicated to reproduce it
1212 // here (see LyXTabular::TeXCellPreamble()).
1213 // Therefore we simply put everything in the
1217 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1218 cellinfo[row][col].align = ci.align;
1219 cellinfo[row][col].special = ci.special;
1220 cellinfo[row][col].leftlines = ci.leftlines;
1221 cellinfo[row][col].rightlines = ci.rightlines;
1223 parse_text_in_inset(parse, os2, FLAG_ITEM, false, context);
1224 if (!cellinfo[row][col].content.empty()) {
1225 // This may or may not work in LaTeX,
1226 // but it does not work in LyX.
1227 // FIXME: Handle it correctly!
1228 cerr << "Moving cell content '"
1230 << "' into a multicolumn cell. "
1231 "This will probably not work."
1234 cellinfo[row][col].content += os2.str();
1236 // add dummy cells for multicol
1237 for (size_t i = 0; i < ncells - 1; ++i) {
1239 if (col >= colinfo.size()) {
1240 cerr << "The cell '"
1243 << convert<string>(ncells)
1244 << " columns while the table has only "
1245 << convert<string>(colinfo.size())
1247 << " Therefore the surplus columns will be ignored."
1251 cellinfo[row][col].multi = CELL_PART_OF_MULTICOLUMN;
1252 cellinfo[row][col].align = 'c';
1255 } else if (col == 0 && colinfo.size() > 1 &&
1257 parse.next_token().cs() == "caption") {
1258 // longtable caption support in LyX is a hack:
1259 // Captions require a row of their own with
1260 // the caption flag set to true, having only
1261 // one multicolumn cell. The contents of that
1262 // cell must contain exactly one caption inset
1263 // and nothing else.
1264 // Fortunately, the caption flag is only needed
1265 // for tables with more than one column.
1266 rowinfo[row].caption = true;
1267 for (size_t c = 1; c < cells.size(); ++c) {
1268 if (!cells[c].empty()) {
1269 cerr << "Moving cell content '"
1271 << "' into the caption cell. "
1272 "This will probably not work."
1274 cells[0] += cells[c];
1278 cellinfo[row][col].align = colinfo[col].align;
1279 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1281 parse_text_in_inset(parse, os2, FLAG_CELL, false, context);
1282 cellinfo[row][col].content += os2.str();
1283 // add dummy multicolumn cells
1284 for (size_t c = 1; c < colinfo.size(); ++c)
1285 cellinfo[row][c].multi = CELL_PART_OF_MULTICOLUMN;
1289 if (parse.next_token().cs() == "begin") {
1290 parse.pushPosition();
1292 string const env = parse.getArg('{', '}');
1293 if (env == "sideways" || env == "turn") {
1294 string angle = "90";
1295 if (env == "turn") {
1297 angle = parse.getArg('{', '}');
1299 active_environments.push_back(env);
1300 parse.ertEnvironment(env);
1301 active_environments.pop_back();
1302 parse.skip_spaces();
1303 if (!parse.good() && support::isStrInt(angle))
1304 rotate = convert<int>(angle);
1306 parse.popPosition();
1308 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1309 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1310 cellinfo[row][col].align = colinfo[col].align;
1313 cellinfo[row][col].rotate = rotate;
1315 active_environments.push_back(parse.getArg('{', '}'));
1317 parse.getArg('{', '}');
1318 parse_text_in_inset(parse, os2, FLAG_END, false, context);
1319 active_environments.pop_back();
1320 preamble.registerAutomaticallyLoadedPackage("rotating");
1322 parse_text_in_inset(parse, os2, FLAG_CELL, false, context);
1324 cellinfo[row][col].content += os2.str();
1328 //cerr << "// handle almost empty last row what we have\n";
1329 // handle almost empty last row
1330 if (row && lines[row].empty() && row + 1 == rowinfo.size()) {
1331 //cerr << "remove empty last line\n";
1332 if (rowinfo[row].topline)
1333 rowinfo[row - 1].bottomline = true;
1334 for (size_t col = 0; col < colinfo.size(); ++col)
1335 if (cellinfo[row][col].topline)
1336 cellinfo[row - 1][col].bottomline = true;
1343 // Now we have the table structure and content in rowinfo, colinfo
1345 // Unfortunately LyX has some limitations that we need to work around.
1348 for (size_t row = 0; row < rowinfo.size(); ++row) {
1349 for (size_t col = 0; col < cellinfo[row].size(); ++col) {
1350 // Convert cells with special content to multicolumn cells
1351 // (LyX ignores the special field for non-multicolumn cells).
1352 if (cellinfo[row][col].multi == CELL_NORMAL &&
1353 !cellinfo[row][col].special.empty())
1354 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1355 // Add multirow dummy cells
1356 if (row > 1 && (cellinfo[row - 1][col].multi == CELL_PART_OF_MULTIROW
1357 || cellinfo[row - 1][col].multi == CELL_BEGIN_OF_MULTIROW)
1358 && cellinfo[row - 1][col].mrxnum > 0) {
1359 // add dummy cells for multirow
1360 cellinfo[row][col].multi = CELL_PART_OF_MULTIROW;
1361 cellinfo[row][col].align = 'c';
1362 cellinfo[row][col].mrxnum = cellinfo[row - 1][col].mrxnum - 1;
1367 // Distribute lines from rows/columns to cells
1368 // The code was stolen from convert_tablines() in lyx2lyx/lyx_1_6.py.
1369 // Each standard cell inherits the settings of the corresponding
1370 // rowinfo/colinfo. This works because all cells with individual
1371 // settings were converted to multicolumn cells above.
1372 // Each multicolumn cell inherits the settings of the rowinfo/colinfo
1373 // corresponding to the first column of the multicolumn cell (default
1374 // of the multicol package). This works because the special field
1375 // overrides the line fields.
1376 for (size_t row = 0; row < rowinfo.size(); ++row) {
1377 for (size_t col = 0; col < cellinfo[row].size(); ++col) {
1378 if (cellinfo[row][col].multi == CELL_NORMAL) {
1379 cellinfo[row][col].topline = rowinfo[row].topline;
1380 cellinfo[row][col].bottomline = rowinfo[row].bottomline;
1381 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1382 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1383 } else if (cellinfo[row][col].multi == CELL_BEGIN_OF_MULTICOLUMN) {
1385 while (s < cellinfo[row].size() &&
1386 cellinfo[row][s].multi == CELL_PART_OF_MULTICOLUMN)
1388 if (s < cellinfo[row].size() &&
1389 cellinfo[row][s].multi != CELL_BEGIN_OF_MULTICOLUMN)
1390 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1391 if (col > 0 && cellinfo[row][col-1].multi == CELL_NORMAL)
1392 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1393 } else if (cellinfo[row][col].multi == CELL_BEGIN_OF_MULTIROW) {
1395 while (s < rowinfo.size() &&
1396 cellinfo[s][col].multi == CELL_PART_OF_MULTIROW)
1398 if (s < cellinfo[row].size() &&
1399 cellinfo[s][col].multi != CELL_BEGIN_OF_MULTIROW)
1400 cellinfo[row][col].bottomline = rowinfo[row].bottomline;
1401 if (row > 0 && cellinfo[row - 1][col].multi == CELL_NORMAL)
1402 cellinfo[row][col].topline = rowinfo[row].topline;
1408 preamble.registerAutomaticallyLoadedPackage("booktabs");
1409 if (name == "longtable")
1410 preamble.registerAutomaticallyLoadedPackage("longtable");
1411 else if (name == "xltabular")
1412 preamble.registerAutomaticallyLoadedPackage("xltabular");
1413 else if (name == "tabularx")
1414 preamble.registerAutomaticallyLoadedPackage("tabularx");
1416 //cerr << "// output what we have\n";
1417 // output what we have
1418 string const rotate = "0";
1419 os << "\n<lyxtabular version=\"3\" rows=\"" << rowinfo.size()
1420 << "\" columns=\"" << colinfo.size() << "\">\n";
1422 << write_attribute("rotate", rotate)
1423 << write_attribute("booktabs", booktabs)
1424 << write_attribute("islongtable", is_long_tabular);
1425 if (is_long_tabular) {
1426 os << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1427 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1428 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1429 << write_attribute("headTopDL", endhead.topDL)
1430 << write_attribute("headBottomDL", endhead.bottomDL)
1431 << write_attribute("footTopDL", endfoot.topDL)
1432 << write_attribute("footBottomDL", endfoot.bottomDL)
1433 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1434 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1435 << write_attribute("lastFootEmpty", endlastfoot.empty);
1436 if (!halign.empty())
1437 os << write_attribute("longtabularalignment", halign);
1439 os << write_attribute("tabularvalignment", tabularvalignment);
1441 os << write_attribute("tabularwidth", tabularwidth) << ">\n";
1443 //cerr << "// after header\n";
1444 for (size_t col = 0; col < colinfo.size(); ++col) {
1445 os << "<column alignment=\""
1446 << verbose_align(colinfo[col].align) << "\""
1448 << verbose_valign(colinfo[col].valign) << "\""
1449 << write_attribute("width", translate_len(colinfo[col].width))
1450 << write_attribute("special", colinfo[col].special)
1451 << write_attribute("varwidth", colinfo[col].varwidth)
1454 //cerr << "// after cols\n";
1456 for (size_t row = 0; row < rowinfo.size(); ++row) {
1458 << write_attribute("topspace", rowinfo[row].top_space)
1459 << write_attribute("bottomspace", rowinfo[row].bottom_space)
1460 << write_attribute("interlinespace", rowinfo[row].interline_space)
1461 << write_attribute("endhead",
1462 rowinfo[row].type == LT_HEAD)
1463 << write_attribute("endfirsthead",
1464 rowinfo[row].type == LT_FIRSTHEAD)
1465 << write_attribute("endfoot",
1466 rowinfo[row].type == LT_FOOT)
1467 << write_attribute("endlastfoot",
1468 rowinfo[row].type == LT_LASTFOOT)
1469 << write_attribute("newpage", rowinfo[row].newpage)
1470 << write_attribute("caption", rowinfo[row].caption)
1472 for (size_t col = 0; col < colinfo.size(); ++col) {
1473 CellInfo const & cell = cellinfo[row][col];
1475 if (cell.multi == CELL_BEGIN_OF_MULTICOLUMN
1476 || cell.multi == CELL_PART_OF_MULTICOLUMN)
1477 os << " multicolumn=\"" << cell.multi << "\"";
1478 if (cell.multi == CELL_BEGIN_OF_MULTIROW
1479 || cell.multi == CELL_PART_OF_MULTIROW)
1480 os << " multirow=\"" << cell.multi << "\"";
1481 os << " alignment=\"" << verbose_align(cell.align)
1483 << " valignment=\"" << verbose_valign(cell.valign)
1485 << write_attribute("topline", cell.topline)
1486 << write_attribute("bottomline", cell.bottomline)
1487 << write_attribute("leftline", cell.leftlines > 0)
1488 << write_attribute("rightline", cell.rightlines > 0)
1489 << write_attribute("rotate", cell.rotate)
1490 << write_attribute("mroffset", cell.mroffset);
1491 //cerr << "\nrow: " << row << " col: " << col;
1493 // cerr << " topline=\"true\"";
1494 //if (cell.bottomline)
1495 // cerr << " bottomline=\"true\"";
1496 os << " usebox=\"none\""
1497 << write_attribute("width", translate_len(cell.width));
1498 if (cell.multi != CELL_NORMAL)
1499 os << write_attribute("special", cell.special);
1501 << "\n\\begin_inset Text\n"
1503 << "\n\\end_inset\n"
1509 os << "</lyxtabular>\n";