将 Java 字符串数组转换为 LaTeX 表

发布于 2024-08-11 16:03:22 字数 490 浏览 3 评论 0原文

我有一个包含字符串的二维数组,并想用它们创建一个 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 中是否有一种聪明的方法来进行此类转换?

I have an two dimension array that contains strings and want to create a LaTeX table with them.
The problem is that they could be longer and there has to be line breakes after 15 characters. The

Say it's a 2x2 array with contains {{"String11.......String11", "String21"}, {"String12.......String12.......String12.......", "String22......String22......"}} then the result should look like:

\begin{tabular}{cc}
String11.......& String21//
String11       &//
\hline
String12.......&String22......//
String12.......&String22......//
String12.......&  
\end{tabular}

Is there a smart way to do such conversion in Java?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明明#如月 2024-08-18 16:03:22

最快的方法是在表格单元格中设置 15em 文本换行,这可能是您最终寻找的解决方案:

\begin{tabular}{p{15em} p{15em}}
String11 & String21 \\
\hline
String12 & String22 \\
\end{tabular}

Alternatively, you could probably find a way to make something like this work:

// regex to place a new line delimiter (eg. \newline) after every 15 characters
string11.replaceAll(".{15}", "$0" + someNewLineDelimiter);

Doing some Google searches on the subject, I see that \newline within a LaTeX table doesn't work as easily as we might want it to, but you should be able to find something that works. The alternative of spanning a string across multiple rows seems like a poor solution.

The quickest way is to just set up 15em text wrapping in your table's cells, which is probably the solution you're ultimately looking for:

\begin{tabular}{p{15em} p{15em}}
String11 & String21 \\
\hline
String12 & String22 \\
\end{tabular}

Alternatively, you could probably find a way to make something like this work:

// regex to place a new line delimiter (eg. \newline) after every 15 characters
string11.replaceAll(".{15}", "$0" + someNewLineDelimiter);

Doing some Google searches on the subject, I see that \newline within a LaTeX table doesn't work as easily as we might want it to, but you should be able to find something that works. The alternative of spanning a string across multiple rows seems like a poor solution.

漫漫岁月 2024-08-18 16:03:22

我不得不承认这个解决方案对我来说看起来很难看。我认为有很多语言可以用更少的代码和错误来完成这项任务。我怀疑 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;
   }

}

I have to admit this solution looks ugly to me. I think there are lots of languages where this task could have been done with far less code and blundering around. I suspect it would come to about 10 lines in Perl, or 2 lines in J. Still, here's the best I was able to come up with:

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;
   }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文