我有一个包含字符串的二维数组,并希望用它们创建一个LaTeX表。 问题是它们可能会更长,并且在15个字符后必须有换行符。
说它是一个2x2数组,包含{{“String11 ....... String11”,“String21”},{“String12 ....... String12 ....... String12 .. .....“,”String22 ...... String22 ......“}}然后结果看起来像:
\begin{tabular}{cc}
String11.......& String21//
String11 &//
\hline
String12.......&String22......//
String12.......&String22......//
String12.......&
\end{tabular}
在Java中有这种转换的智能方法吗?
答案 0 :(得分:3)
最快捷的方法是在表格的单元格中设置15em文本换行,这可能是您最终要寻找的解决方案:
\begin{tabular}{p{15em} p{15em}}
String11 & String21 \\
\hline
String12 & String22 \\
\end{tabular}
或者,您可能会找到一种方法来制作类似这样的工作:
String11 & String21 \\
\hline
String12 & String22 \\
\end{tabular}
对这个主题进行了一些谷歌搜索,我发现LaTeX表中的// regex to place a new line delimiter (eg. \newline) after every 15 characters
string11.replaceAll(".{15}", "$0" + someNewLineDelimiter);
并不像我们想要的那样容易,但你应该能找到有用的东西。跨越多行的字符串替代方案似乎是一个糟糕的解决方案。
答案 1 :(得分:1)
我不得不承认这个解决方案看起来很难看。我认为有很多语言可以用更少的代码和浮躁来完成这项任务。我怀疑它会在Perl中出现大约10行,或者在J.中出现2行。不过,这是我能想到的最好的行:
import java.util.ArrayList;
import java.util.List;
public class Christian {
private static final int COLUMNS = 2, CELL_WIDTH = 15;
private static final String[][] array = {
{"I have an two dimension array that contains strings and want to create a LaTeX table with them. T",
"roblem is that they could be longer and there has to be line breakes after 15 characters. Th"},
{"Say it's a 2x2 array with contains {{'String11.......String11', 'String21'}",
"{'String12.......String12.......String12.......', 'String22......String22......'}} then the result should look like"}
};
public static void main(String[] args) {
ArrayList<String> lines = new ArrayList<String>();
lines.add("\\begin{tabular}{cc}");
for (String[] row : array) {
lines.addAll(convertRow(row));
lines.add("\\hline");
}
lines.remove(lines.size() - 1);
lines.add("\\end{tabular}");
for (String line : lines) {
System.out.println(line);
}
}
private static List<String> convertRow(String[] row) {
ArrayList<ArrayList<String>> rearranged = new ArrayList<ArrayList<String>>();
int deepest = 0;
for (int col=0; col<row.length; col++) {
boolean last = (col == row.length - 1);
String marker = (last) ? "//" : "&";
String text = row[col];
ArrayList<String> cell = new ArrayList<String>();
for (int z=0; z<text.length(); z+=15) {
String cc = (z+CELL_WIDTH < text.length()) ? text.substring(z, z+CELL_WIDTH) : text.substring(z);
cell.add(cc + marker);
deepest = Math.max(deepest, cell.size());
}
rearranged.add(cell);
}
for (int col=0; col<row.length; col++) {
ArrayList<String> cell = rearranged.get(col);
boolean last = (col == row.length - 1);
String marker = (last) ? "//" : "&";
while (cell.size() < deepest) cell.add(marker);
}
ArrayList<String> lines = new ArrayList<String>();
for (int line=0; line<deepest; line++) {
StringBuilder sb = new StringBuilder();
for (int col=0; col<row.length; col++) {
sb.append(rearranged.get(col).get(line));
}
boolean last = (line == deepest - 1);
if (last) sb.delete(sb.length() - 2, sb.length());
lines.add(sb.toString());
}
return lines;
}
}