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"
39 ColInfo() : align('n'), valign('n'), rightlines(0), leftlines(0) {}
42 /// vertical alignment
46 /// special column alignment
48 /// number of lines on the right
50 /// number of lines on the left
55 /// row type for longtables
62 /// part of head on first page
66 /// part of foot on last page
73 RowInfo() : topline(false), bottomline(false), type(LT_NORMAL),
74 caption(false), newpage(false) {}
75 /// Does this row have any special setting?
78 return topline || bottomline || !top_space.empty() ||
79 !bottom_space.empty() || !interline_space.empty() ||
80 type != LT_NORMAL || caption || newpage;
82 /// horizontal line above
84 /// horizontal line below
86 /// Extra space between the top line and this row
88 /// Extra space between this row and the bottom line
90 /// Extra space between the bottom line and the next top line
91 string interline_space;
92 /// These are for longtabulars only
93 /// row type (head, foot, firsthead etc.)
102 /// the numeric values are part of the file format!
106 /// A multicolumn cell. The number of columns is <tt>1 + number
107 /// of CELL_PART_OF_MULTICOLUMN cells</tt> that follow directly
108 CELL_BEGIN_OF_MULTICOLUMN = 1,
109 /// This is a dummy cell (part of a multicolumn cell)
110 CELL_PART_OF_MULTICOLUMN = 2
116 CellInfo() : multi(CELL_NORMAL), align('n'), valign('n'),
117 leftlines(0), rightlines(0), topline(false),
118 bottomline(false), rotate(0) {}
125 /// vertical cell alignment
127 /// number of lines on the left
129 /// number of lines on the right
131 /// do we have a line above?
133 /// do we have a line below?
135 /// how is the cell rotated?
137 /// width for multicolumn cells
139 /// special formatting for multicolumn cells
147 ltType() : set(false), topDL(false), bottomDL(false), empty(false) {}
148 // we have this header type (is set in the getLT... functions)
150 // double borders on top
152 // double borders on bottom
154 // used for FirstHeader & LastFooter and if this is true
155 // all the rows marked as FirstHeader or LastFooter are
156 // ignored in the output and it is set to be empty!
161 /// translate a horizontal alignment (as stored in ColInfo and CellInfo) to LyX
162 inline char const * verbose_align(char c)
177 /// translate a vertical alignment (as stored in ColInfo and CellInfo) to LyX
178 inline char const * verbose_valign(char c)
180 // The default value for no special alignment is "top".
193 // stripped down from tabluar.C. We use it currently only for bools and
195 string const write_attribute(string const & name, bool const & b)
197 // we write only true attribute values so we remove a bit of the
198 // file format bloat for tabulars.
199 return b ? ' ' + name + "=\"true\"" : string();
203 string const write_attribute(string const & name, string const & s)
205 return s.empty() ? string() : ' ' + name + "=\"" + s + '"';
209 string const write_attribute(string const & name, int const & i)
211 // we write only true attribute values so we remove a bit of the
212 // file format bloat for tabulars.
213 return i ? write_attribute(name, convert<string>(i)) : string();
217 /*! rather brutish way to code table structure in a string:
222 \multicolumn{2}{c}{4} & 5 //
228 gets "translated" to:
231 HLINE 1 TAB 2 TAB 3 HLINE HLINE LINE
232 \hline HLINE \multicolumn{2}{c}{4} TAB 5 HLINE HLINE LINE
233 HLINE 6 TAB 7 HLINE HLINE LINE
234 HLINE 8 HLINE \endhead HLINE LINE
238 char const TAB = '\001';
239 char const LINE = '\002';
240 char const HLINE = '\004';
244 * Move the information in leftlines, rightlines, align and valign to the
245 * special field. This is necessary if the special field is not empty,
246 * because LyX ignores leftlines > 1, rightlines > 1, align and valign in
249 void ci2special(ColInfo & ci)
251 if (ci.width.empty() && ci.align == 'n')
252 // The alignment setting is already in special, since
253 // handle_colalign() never stores ci with these settings
254 // and ensures that leftlines == 0 and rightlines == 0 in
258 if (!ci.width.empty()) {
261 ci.special += ">{\\raggedright}";
264 ci.special += ">{\\raggedleft}";
267 ci.special += ">{\\centering}";
270 if (ci.valign == 'n')
273 ci.special += ci.valign;
274 ci.special += '{' + ci.width + '}';
277 ci.special += ci.align;
279 // LyX can only have one left and one right line.
280 for (int i = 1; i < ci.leftlines; ++i)
281 ci.special.insert(0, "|");
282 for (int i = 1; i < ci.rightlines; ++i)
284 ci.leftlines = min(ci.leftlines, 1);
285 ci.rightlines = min(ci.rightlines, 1);
292 * Handle column specifications for tabulars and multicolumns.
293 * The next token of the parser \p p must be an opening brace, and we read
294 * everything until the matching closing brace.
295 * The resulting column specifications are filled into \p colinfo. This is
296 * in an intermediate form. fix_colalign() makes it suitable for LyX output.
298 void handle_colalign(Parser & p, vector<ColInfo> & colinfo,
299 ColInfo const & start)
301 if (p.get_token().cat() != catBegin)
302 cerr << "Wrong syntax for table column alignment.\n"
303 "Expected '{', got '" << p.curr_token().asInput()
306 ColInfo next = start;
307 for (Token t = p.get_token(); p.good() && t.cat() != catEnd;
310 cerr << "t: " << t << " c: '" << t.character() << "'\n";
313 // We cannot handle comments here
314 if (t.cat() == catComment) {
315 if (t.cs().empty()) {
319 cerr << "Ignoring comment: " << t.asInput();
323 switch (t.character()) {
327 // new column, horizontal aligned
328 next.align = t.character();
329 if (!next.special.empty())
331 colinfo.push_back(next);
337 // new column, vertical aligned box
338 next.valign = t.character();
339 next.width = p.verbatim_item();
340 if (!next.special.empty())
342 colinfo.push_back(next);
347 if (colinfo.empty()) {
348 if (next.special.empty())
352 } else if (colinfo.back().special.empty())
353 ++colinfo.back().rightlines;
354 else if (next.special.empty())
357 colinfo.back().special += '|';
360 // text before the next column
361 string const s = trimSpaceAndEol(p.verbatim_item());
362 if (next.special.empty() &&
364 // Maybe this can be converted to a
365 // horizontal alignment setting for
366 // fixed width columns
367 if (s == "\\raggedleft")
369 else if (s == "\\raggedright")
371 else if (s == "\\centering")
374 next.special = ">{" + s + '}';
376 next.special += ">{" + s + '}';
380 // text after the last column
381 string const s = trimSpaceAndEol(p.verbatim_item());
383 // This is not possible in LaTeX.
384 cerr << "Ignoring separator '<{"
385 << s << "}'." << endl;
387 ColInfo & ci = colinfo.back();
389 ci.special += "<{" + s + '}';
394 // *{n}{arg} means 'n' columns of type 'arg'
395 string const num = p.verbatim_item();
396 string const arg = p.verbatim_item();
397 size_t const n = convert<unsigned int>(num);
398 if (!arg.empty() && n > 0) {
400 for (size_t i = 0; i < n; ++i)
404 handle_colalign(p2, colinfo, next);
407 cerr << "Ignoring column specification"
408 " '*{" << num << "}{"
409 << arg << "}'." << endl;
414 // text instead of the column spacing
416 // text in addition to the column spacing
417 next.special += t.character();
418 next.special += '{' + p.verbatim_item() + '}';
421 // try user defined column types
422 // unknown column types (nargs == -1) are
423 // assumed to consume no arguments
425 next.special += t.character();
427 preamble.getSpecialTableColumnArguments(t.character());
428 for (int i = 0; i < nargs; ++i)
429 next.special += '{' +
430 p.verbatim_item() + '}';
431 colinfo.push_back(next);
438 // Maybe we have some column separators that need to be added to the
441 if (!next.special.empty()) {
442 ColInfo & ci = colinfo.back();
444 ci.special += next.special;
445 next.special.erase();
451 * Move the left and right lines and alignment settings of the column \p ci
452 * to the special field if necessary.
454 void fix_colalign(ColInfo & ci)
456 if (ci.leftlines > 1 || ci.rightlines > 1)
462 * LyX can't handle more than one vertical line at the left or right side
464 * This function moves the left and right lines and alignment settings of all
465 * columns in \p colinfo to the special field if necessary.
467 void fix_colalign(vector<ColInfo> & colinfo)
469 // Try to move extra leftlines to the previous column.
470 // We do this only if both special fields are empty, otherwise we
471 // can't tell wether the result will be the same.
472 for (size_t col = 0; col < colinfo.size(); ++col) {
473 if (colinfo[col].leftlines > 1 &&
474 colinfo[col].special.empty() && col > 0 &&
475 colinfo[col - 1].rightlines == 0 &&
476 colinfo[col - 1].special.empty()) {
477 ++colinfo[col - 1].rightlines;
478 --colinfo[col].leftlines;
481 // Try to move extra rightlines to the next column
482 for (size_t col = 0; col < colinfo.size(); ++col) {
483 if (colinfo[col].rightlines > 1 &&
484 colinfo[col].special.empty() &&
485 col < colinfo.size() - 1 &&
486 colinfo[col + 1].leftlines == 0 &&
487 colinfo[col + 1].special.empty()) {
488 ++colinfo[col + 1].leftlines;
489 --colinfo[col].rightlines;
492 // Move the lines and alignment settings to the special field if
494 for (size_t col = 0; col < colinfo.size(); ++col)
495 fix_colalign(colinfo[col]);
500 * Parse hlines and similar stuff.
501 * \returns wether the token \p t was parsed
503 bool parse_hlines(Parser & p, Token const & t, string & hlines,
504 bool is_long_tabular)
506 LASSERT(t.cat() == catEscape, return false);
508 if (t.cs() == "hline" || t.cs() == "toprule" || t.cs() == "midrule" ||
509 t.cs() == "bottomrule")
510 hlines += '\\' + t.cs();
512 else if (t.cs() == "cline")
513 hlines += "\\cline{" + p.verbatim_item() + '}';
515 else if (t.cs() == "cmidrule") {
516 // We cannot handle the \cmidrule(l){3-4} form
519 bool const hasParentheses(p.getFullArg('(', ')').first);
523 hlines += "\\cmidrule{" + p.verbatim_item() + '}';
526 else if (t.cs() == "addlinespace") {
529 bool const hasArgument(p.getFullArg('{', '}').first);
532 hlines += "\\addlinespace{" + p.verbatim_item() + '}';
534 hlines += "\\addlinespace";
537 else if (is_long_tabular && t.cs() == "newpage")
538 hlines += "\\newpage";
547 /// Position in a row
549 /// At the very beginning, before the first token
551 /// After the first token and before any column token
553 /// After the first column token. Comments and whitespace are only
554 /// treated as tokens in this position
556 /// After the first non-column token at the end
562 * Parse table structure.
563 * We parse tables in a two-pass process: This function extracts the table
564 * structure (rows, columns, hlines etc.), but does not change the cell
565 * content. The cell content is parsed in a second step in handle_tabular().
567 void parse_table(Parser & p, ostream & os, bool is_long_tabular,
568 RowPosition & pos, unsigned flags)
570 // table structure commands such as \hline
573 // comments that occur at places where we can't handle them
577 Token const & t = p.get_token();
580 debugToken(cerr, t, flags);
583 // comments and whitespace in hlines
586 case IN_HLINES_START:
588 if (t.cat() == catComment) {
593 // We can't handle comments here,
594 // store them for later use
595 comments += t.asInput();
597 } else if (t.cat() == catSpace ||
598 t.cat() == catNewline) {
599 // whitespace is irrelevant here, we
600 // need to recognize hline stuff
609 // We need to handle structure stuff first in order to
610 // determine wether we need to output a HLINE separator
611 // before the row or not.
612 if (t.cat() == catEscape) {
613 if (parse_hlines(p, t, hlines, is_long_tabular)) {
616 pos = IN_HLINES_START;
621 case IN_HLINES_START:
628 else if (t.cs() == "tabularnewline" ||
632 cerr << "Warning: Converting TeX "
633 "'\\cr' to LaTeX '\\\\'."
635 // stuff before the line break
636 os << comments << HLINE << hlines << HLINE
638 //cerr << "hlines: " << hlines << endl;
645 else if (is_long_tabular &&
646 (t.cs() == "endhead" ||
647 t.cs() == "endfirsthead" ||
648 t.cs() == "endfoot" ||
649 t.cs() == "endlastfoot")) {
650 hlines += t.asInput();
654 // these commands are implicit line
656 os << comments << HLINE << hlines
663 pos = IN_HLINES_START;
665 case IN_HLINES_START:
672 // We need a HLINE separator if we either have no hline
673 // stuff at all and are just starting a row or if we just
674 // got the first non-hline token.
677 // no hline tokens exist, first token at row start
678 case IN_HLINES_START:
679 // hline tokens exist, first non-hline token at row
681 os << hlines << HLINE << comments;
687 // Oops, there is still cell content or unsupported
688 // booktabs commands after hline stuff. The latter are
689 // moved to the cell, and the first does not work in
690 // LaTeX, so we ignore the hlines.
693 if (support::contains(hlines, "\\hline") ||
694 support::contains(hlines, "\\cline") ||
695 support::contains(hlines, "\\newpage"))
696 cerr << "Ignoring '" << hlines
697 << "' in a cell" << endl;
707 // If we come here we have normal cell content
711 if (t.cat() == catMath) {
712 // we are inside some text mode thingy, so opening new math is allowed
713 Token const & n = p.get_token();
714 if (n.cat() == catMath) {
715 // TeX's $$...$$ syntax for displayed math
717 // This does only work because parse_math outputs TeX
718 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
720 p.get_token(); // skip the second '$' token
722 // simple $...$ stuff
725 // This does only work because parse_math outputs TeX
726 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
731 else if (t.cat() == catSpace
732 || t.cat() == catNewline
733 || t.cat() == catLetter
734 || t.cat() == catSuper
736 || t.cat() == catOther
737 || t.cat() == catActive
738 || t.cat() == catParameter)
741 else if (t.cat() == catBegin) {
743 parse_table(p, os, is_long_tabular, pos,
748 else if (t.cat() == catEnd) {
749 if (flags & FLAG_BRACE_LAST)
751 cerr << "unexpected '}'\n";
754 else if (t.cat() == catAlign) {
759 else if (t.cat() == catComment)
762 else if (t.cs() == "(") {
764 // This does only work because parse_math outputs TeX
765 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
769 else if (t.cs() == "[") {
771 // This does only work because parse_math outputs TeX
772 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
776 else if (t.cs() == "begin") {
777 string const name = p.getArg('{', '}');
778 active_environments.push_back(name);
779 os << "\\begin{" << name << '}';
780 // treat the nested environment as a block, don't
781 // parse &, \\ etc, because they don't belong to our
782 // table if they appear.
783 os << p.ertEnvironment(name);
784 os << "\\end{" << name << '}';
785 active_environments.pop_back();
788 else if (t.cs() == "end") {
789 if (flags & FLAG_END) {
790 // eat environment name
791 string const name = p.getArg('{', '}');
792 if (name != active_environment())
793 p.error("\\end{" + name + "} does not match \\begin{"
794 + active_environment() + "}");
797 p.error("found 'end' unexpectedly");
804 // We can have comments if the last line is incomplete
807 // We can have hline stuff if the last line is incomplete
808 if (!hlines.empty()) {
809 // this does not work in LaTeX, so we ignore it
810 cerr << "Ignoring '" << hlines << "' at end of tabular"
816 void handle_hline_above(RowInfo & ri, vector<CellInfo> & ci)
819 for (size_t col = 0; col < ci.size(); ++col)
820 ci[col].topline = true;
824 void handle_hline_below(RowInfo & ri, vector<CellInfo> & ci)
826 ri.bottomline = true;
827 for (size_t col = 0; col < ci.size(); ++col)
828 ci[col].bottomline = true;
832 } // anonymous namespace
835 void handle_tabular(Parser & p, ostream & os, string const & name,
836 string const & tabularwidth, Context & context)
838 bool const is_long_tabular(name == "longtable");
839 bool booktabs = false;
840 string tabularvalignment("middle");
841 string posopts = p.getOpt();
842 if (!posopts.empty()) {
843 // FIXME: Convert this to ERT
845 cerr << "horizontal longtable positioning '"
846 << posopts << "' ignored\n";
847 else if (posopts == "[t]")
848 tabularvalignment = "top";
849 else if (posopts == "[b]")
850 tabularvalignment = "bottom";
852 cerr << "vertical tabular positioning '"
853 << posopts << "' ignored\n";
856 vector<ColInfo> colinfo;
858 // handle column formatting
859 handle_colalign(p, colinfo, ColInfo());
860 fix_colalign(colinfo);
862 // first scan of cells
863 // use table mode to keep it minimal-invasive
864 // not exactly what's TeX doing...
865 vector<string> lines;
867 RowPosition rowpos = ROW_START;
868 parse_table(p, ss, is_long_tabular, rowpos, FLAG_END);
869 split(ss.str(), lines, LINE);
871 vector< vector<CellInfo> > cellinfo(lines.size());
872 vector<RowInfo> rowinfo(lines.size());
879 //cerr << "// split into rows\n";
880 for (size_t row = 0; row < rowinfo.size();) {
883 cellinfo[row].resize(colinfo.size());
884 bool deletelastrow = false;
887 vector<string> dummy;
888 //cerr << "\n########### LINE: " << lines[row] << "########\n";
889 split(lines[row], dummy, HLINE);
891 // handle horizontal line fragments
892 // we do only expect this for a last line without '\\'
893 if (dummy.size() != 3) {
894 if ((dummy.size() != 1 && dummy.size() != 2) ||
895 row != rowinfo.size() - 1)
896 cerr << "unexpected dummy size: " << dummy.size()
897 << " content: " << lines[row] << "\n";
900 lines[row] = dummy[1];
902 //cerr << "line: " << row << " above 0: " << dummy[0] << "\n";
903 //cerr << "line: " << row << " below 2: " << dummy[2] << "\n";
904 //cerr << "line: " << row << " cells 1: " << dummy[1] << "\n";
906 for (int i = 0; i <= 2; i += 2) {
907 //cerr << " reading from line string '" << dummy[i] << "'\n";
910 Token t = p1.get_token();
911 //cerr << "read token: " << t << "\n";
912 if (t.cs() == "hline" || t.cs() == "toprule" ||
913 t.cs() == "midrule" ||
914 t.cs() == "bottomrule") {
915 if (t.cs() != "hline")
918 if (rowinfo[row].topline) {
919 if (row > 0) // extra bottomline above
920 handle_hline_below(rowinfo[row - 1], cellinfo[row - 1]);
922 cerr << "dropping extra "
924 //cerr << "below row: " << row-1 << endl;
926 handle_hline_above(rowinfo[row], cellinfo[row]);
927 //cerr << "above row: " << row << endl;
930 //cerr << "below row: " << row << endl;
931 handle_hline_below(rowinfo[row], cellinfo[row]);
933 } else if (t.cs() == "cline" || t.cs() == "cmidrule") {
934 if (t.cs() == "cmidrule")
936 string arg = p1.verbatim_item();
937 //cerr << "read " << t.cs() << " arg: '" << arg << "'\n";
939 split(arg, cols, '-');
941 size_t from = convert<unsigned int>(cols[0]);
943 cerr << "Could not parse "
944 << t.cs() << " start column."
947 // 1 based index -> 0 based
949 if (from >= colinfo.size()) {
950 cerr << t.cs() << " starts at "
951 "non existing column "
952 << (from + 1) << endl;
953 from = colinfo.size() - 1;
955 size_t to = convert<unsigned int>(cols[1]);
957 cerr << "Could not parse "
958 << t.cs() << " end column."
961 // 1 based index -> 0 based
963 if (to >= colinfo.size()) {
964 cerr << t.cs() << " ends at "
965 "non existing column "
967 to = colinfo.size() - 1;
969 for (size_t col = from; col <= to; ++col) {
970 //cerr << "row: " << row << " col: " << col << " i: " << i << endl;
972 rowinfo[row].topline = true;
973 cellinfo[row][col].topline = true;
975 rowinfo[row].bottomline = true;
976 cellinfo[row][col].bottomline = true;
979 } else if (t.cs() == "addlinespace") {
981 string const opt = p.next_token().cat() == catBegin ?
982 p.verbatim_item() : string();
985 rowinfo[row].top_space = "default";
987 rowinfo[row].top_space = translate_len(opt);
988 } else if (rowinfo[row].bottomline) {
990 rowinfo[row].bottom_space = "default";
992 rowinfo[row].bottom_space = translate_len(opt);
995 rowinfo[row].interline_space = "default";
997 rowinfo[row].interline_space = translate_len(opt);
999 } else if (t.cs() == "endhead") {
1001 endhead.empty = true;
1003 rowinfo[row].type = LT_HEAD;
1004 for (int r = row - 1; r >= 0; --r) {
1005 if (rowinfo[r].type != LT_NORMAL)
1007 rowinfo[r].type = LT_HEAD;
1008 endhead.empty = false;
1011 } else if (t.cs() == "endfirsthead") {
1013 endfirsthead.empty = true;
1015 rowinfo[row].type = LT_FIRSTHEAD;
1016 for (int r = row - 1; r >= 0; --r) {
1017 if (rowinfo[r].type != LT_NORMAL)
1019 rowinfo[r].type = LT_FIRSTHEAD;
1020 endfirsthead.empty = false;
1022 endfirsthead.set = true;
1023 } else if (t.cs() == "endfoot") {
1025 endfoot.empty = true;
1027 rowinfo[row].type = LT_FOOT;
1028 for (int r = row - 1; r >= 0; --r) {
1029 if (rowinfo[r].type != LT_NORMAL)
1031 rowinfo[r].type = LT_FOOT;
1032 endfoot.empty = false;
1035 } else if (t.cs() == "endlastfoot") {
1037 endlastfoot.empty = true;
1039 rowinfo[row].type = LT_LASTFOOT;
1040 for (int r = row - 1; r >= 0; --r) {
1041 if (rowinfo[r].type != LT_NORMAL)
1043 rowinfo[r].type = LT_LASTFOOT;
1044 endlastfoot.empty = false;
1046 endlastfoot.set = true;
1047 } else if (t.cs() == "newpage") {
1050 rowinfo[row - 1].newpage = true;
1052 // This does not work in LaTeX
1058 rowinfo[row].newpage = true;
1060 cerr << "unexpected line token: " << t << endl;
1065 // LyX ends headers and footers always with \tabularnewline.
1066 // This causes one additional row in the output.
1067 // If the last row of a header/footer is empty, we can work
1068 // around that by removing it.
1070 RowInfo test = rowinfo[row-1];
1071 test.type = LT_NORMAL;
1072 if (lines[row-1].empty() && !test.special()) {
1073 switch (rowinfo[row-1].type) {
1075 if (rowinfo[row].type != LT_FIRSTHEAD &&
1076 rowinfo[row-2].type == LT_FIRSTHEAD)
1077 deletelastrow = true;
1080 if (rowinfo[row].type != LT_HEAD &&
1081 rowinfo[row-2].type == LT_HEAD)
1082 deletelastrow = true;
1085 if (rowinfo[row].type != LT_FOOT &&
1086 rowinfo[row-2].type == LT_FOOT)
1087 deletelastrow = true;
1090 if (rowinfo[row].type != LT_LASTFOOT &&
1091 rowinfo[row-2].type == LT_LASTFOOT)
1092 deletelastrow = true;
1100 if (deletelastrow) {
1101 lines.erase(lines.begin() + (row - 1));
1102 rowinfo.erase(rowinfo.begin() + (row - 1));
1103 cellinfo.erase(cellinfo.begin() + (row - 1));
1108 vector<string> cells;
1109 split(lines[row], cells, TAB);
1110 for (size_t col = 0, cell = 0; cell < cells.size();
1112 //cerr << "cell content: '" << cells[cell] << "'\n";
1113 if (col >= colinfo.size()) {
1114 // This does not work in LaTeX
1115 cerr << "Ignoring extra cell '"
1116 << cells[cell] << "'." << endl;
1119 Parser p(cells[cell]);
1121 //cells[cell] << "'\n";
1122 if (p.next_token().cs() == "multicolumn") {
1125 size_t const ncells =
1126 convert<unsigned int>(p.verbatim_item());
1128 // special cell properties alignment
1130 handle_colalign(p, t, ColInfo());
1131 p.skip_spaces(true);
1132 ColInfo & ci = t.front();
1134 // The logic of LyX for multicolumn vertical
1135 // lines is too complicated to reproduce it
1136 // here (see LyXTabular::TeXCellPreamble()).
1137 // Therefore we simply put everything in the
1141 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1142 cellinfo[row][col].align = ci.align;
1143 cellinfo[row][col].special = ci.special;
1144 cellinfo[row][col].leftlines = ci.leftlines;
1145 cellinfo[row][col].rightlines = ci.rightlines;
1147 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
1148 if (!cellinfo[row][col].content.empty()) {
1149 // This may or may not work in LaTeX,
1150 // but it does not work in LyX.
1151 // FIXME: Handle it correctly!
1152 cerr << "Moving cell content '"
1154 << "' into a multicolumn cell. "
1155 "This will probably not work."
1158 cellinfo[row][col].content += os.str();
1160 // add dummy cells for multicol
1161 for (size_t i = 0; i < ncells - 1; ++i) {
1163 if (col >= colinfo.size()) {
1164 cerr << "The cell '"
1167 << convert<string>(ncells)
1168 << " columns while the table has only "
1169 << convert<string>(colinfo.size())
1171 << " Therefore the surplus columns will be ignored."
1175 cellinfo[row][col].multi = CELL_PART_OF_MULTICOLUMN;
1176 cellinfo[row][col].align = 'c';
1179 } else if (col == 0 && colinfo.size() > 1 &&
1181 p.next_token().cs() == "caption") {
1182 // longtable caption support in LyX is a hack:
1183 // Captions require a row of their own with
1184 // the caption flag set to true, having only
1185 // one multicolumn cell. The contents of that
1186 // cell must contain exactly one caption inset
1187 // and nothing else.
1188 // Fortunately, the caption flag is only needed
1189 // for tables with more than one column.
1190 rowinfo[row].caption = true;
1191 for (size_t c = 1; c < cells.size(); ++c) {
1192 if (!cells[c].empty()) {
1193 cerr << "Moving cell content '"
1195 << "' into the caption cell. "
1196 "This will probably not work."
1198 cells[0] += cells[c];
1202 cellinfo[row][col].align = colinfo[col].align;
1203 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1205 parse_text_in_inset(p, os, FLAG_CELL, false, context);
1206 cellinfo[row][col].content += os.str();
1207 // add dummy multicolumn cells
1208 for (size_t c = 1; c < colinfo.size(); ++c)
1209 cellinfo[row][c].multi = CELL_PART_OF_MULTICOLUMN;
1213 if (p.next_token().cs() == "begin") {
1216 string const env = p.getArg('{', '}');
1217 if (env == "sideways" || env == "turn") {
1218 string angle = "90";
1219 if (env == "turn") {
1221 angle = p.getArg('{', '}');
1223 active_environments.push_back(env);
1224 p.ertEnvironment(env);
1225 active_environments.pop_back();
1227 if (!p.good() && support::isStrInt(angle))
1228 rotate = convert<int>(angle);
1232 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1233 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1234 cellinfo[row][col].align = colinfo[col].align;
1237 cellinfo[row][col].rotate = rotate;
1239 active_environments.push_back(p.getArg('{', '}'));
1242 parse_text_in_inset(p, os, FLAG_END, false, context);
1243 active_environments.pop_back();
1244 preamble.registerAutomaticallyLoadedPackage("rotating");
1246 parse_text_in_inset(p, os, FLAG_CELL, false, context);
1248 cellinfo[row][col].content += os.str();
1252 //cerr << "// handle almost empty last row what we have\n";
1253 // handle almost empty last row
1254 if (row && lines[row].empty() && row + 1 == rowinfo.size()) {
1255 //cerr << "remove empty last line\n";
1256 if (rowinfo[row].topline)
1257 rowinfo[row - 1].bottomline = true;
1258 for (size_t col = 0; col < colinfo.size(); ++col)
1259 if (cellinfo[row][col].topline)
1260 cellinfo[row - 1][col].bottomline = true;
1267 // Now we have the table structure and content in rowinfo, colinfo
1269 // Unfortunately LyX has some limitations that we need to work around.
1271 // Convert cells with special content to multicolumn cells
1272 // (LyX ignores the special field for non-multicolumn cells).
1273 for (size_t row = 0; row < rowinfo.size(); ++row) {
1274 for (size_t col = 0; col < cellinfo[row].size(); ++col) {
1275 if (cellinfo[row][col].multi == CELL_NORMAL &&
1276 !cellinfo[row][col].special.empty())
1277 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1281 // Distribute lines from rows/columns to cells
1282 // The code was stolen from convert_tablines() in lyx2lyx/lyx_1_6.py.
1283 // Each standard cell inherits the settings of the corresponding
1284 // rowinfo/colinfo. This works because all cells with individual
1285 // settings were converted to multicolumn cells above.
1286 // Each multicolumn cell inherits the settings of the rowinfo/colinfo
1287 // corresponding to the first column of the multicolumn cell (default
1288 // of the multicol package). This works because the special field
1289 // overrides the line fields.
1290 for (size_t row = 0; row < rowinfo.size(); ++row) {
1291 for (size_t col = 0; col < cellinfo[row].size(); ++col) {
1292 if (cellinfo[row][col].multi == CELL_NORMAL) {
1293 cellinfo[row][col].topline = rowinfo[row].topline;
1294 cellinfo[row][col].bottomline = rowinfo[row].bottomline;
1295 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1296 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1297 } else if (cellinfo[row][col].multi == CELL_BEGIN_OF_MULTICOLUMN) {
1299 while (s < cellinfo[row].size() &&
1300 cellinfo[row][s].multi == CELL_PART_OF_MULTICOLUMN)
1302 if (s < cellinfo[row].size() &&
1303 cellinfo[row][s].multi != CELL_BEGIN_OF_MULTICOLUMN)
1304 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1305 if (col > 0 && cellinfo[row][col-1].multi == CELL_NORMAL)
1306 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1312 preamble.registerAutomaticallyLoadedPackage("booktabs");
1313 if (is_long_tabular)
1314 preamble.registerAutomaticallyLoadedPackage("longtable");
1316 //cerr << "// output what we have\n";
1317 // output what we have
1318 string const rotate = "0";
1319 os << "\n<lyxtabular version=\"3\" rows=\"" << rowinfo.size()
1320 << "\" columns=\"" << colinfo.size() << "\">\n";
1322 << write_attribute("rotate", rotate)
1323 << write_attribute("booktabs", booktabs)
1324 << write_attribute("islongtable", is_long_tabular);
1325 if (is_long_tabular) {
1326 os << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1327 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1328 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1329 << write_attribute("headTopDL", endhead.topDL)
1330 << write_attribute("headBottomDL", endhead.bottomDL)
1331 << write_attribute("footTopDL", endfoot.topDL)
1332 << write_attribute("footBottomDL", endfoot.bottomDL)
1333 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1334 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1335 << write_attribute("lastFootEmpty", endlastfoot.empty);
1337 os << write_attribute("tabularvalignment", tabularvalignment)
1338 << write_attribute("tabularwidth", tabularwidth);
1341 //cerr << "// after header\n";
1342 for (size_t col = 0; col < colinfo.size(); ++col) {
1343 os << "<column alignment=\""
1344 << verbose_align(colinfo[col].align) << "\""
1346 << verbose_valign(colinfo[col].valign) << "\""
1347 << write_attribute("width", translate_len(colinfo[col].width))
1348 << write_attribute("special", colinfo[col].special)
1351 //cerr << "// after cols\n";
1353 for (size_t row = 0; row < rowinfo.size(); ++row) {
1355 << write_attribute("topspace", rowinfo[row].top_space)
1356 << write_attribute("bottomspace", rowinfo[row].bottom_space)
1357 << write_attribute("interlinespace", rowinfo[row].interline_space)
1358 << write_attribute("endhead",
1359 rowinfo[row].type == LT_HEAD)
1360 << write_attribute("endfirsthead",
1361 rowinfo[row].type == LT_FIRSTHEAD)
1362 << write_attribute("endfoot",
1363 rowinfo[row].type == LT_FOOT)
1364 << write_attribute("endlastfoot",
1365 rowinfo[row].type == LT_LASTFOOT)
1366 << write_attribute("newpage", rowinfo[row].newpage)
1367 << write_attribute("caption", rowinfo[row].caption)
1369 for (size_t col = 0; col < colinfo.size(); ++col) {
1370 CellInfo const & cell = cellinfo[row][col];
1372 if (cell.multi != CELL_NORMAL)
1373 os << " multicolumn=\"" << cell.multi << "\"";
1374 os << " alignment=\"" << verbose_align(cell.align)
1376 << " valignment=\"" << verbose_valign(cell.valign)
1378 << write_attribute("topline", cell.topline)
1379 << write_attribute("bottomline", cell.bottomline)
1380 << write_attribute("leftline", cell.leftlines > 0)
1381 << write_attribute("rightline", cell.rightlines > 0)
1382 << write_attribute("rotate", cell.rotate);
1383 //cerr << "\nrow: " << row << " col: " << col;
1385 // cerr << " topline=\"true\"";
1386 //if (cell.bottomline)
1387 // cerr << " bottomline=\"true\"";
1388 os << " usebox=\"none\""
1389 << write_attribute("width", translate_len(cell.width));
1390 if (cell.multi != CELL_NORMAL)
1391 os << write_attribute("special", cell.special);
1393 << "\n\\begin_inset Text\n"
1395 << "\n\\end_inset\n"
1401 os << "</lyxtabular>\n";