错位的嵌套表格在另一个表格内部。

huangapple go评论62阅读模式
英文:

Misaligned nested tabulars inside another tabular

问题

I want to make a table where the hlines start to the second column. In order to do so, I nested tabulars but they don't align well as you can see :

错位的嵌套表格在另一个表格内部。

Is it possible to solve this alignment problem with nested tabulars ? I'd prefer not to use extra libraries unless the code becomes awful to manage.

I'm open to other solutions without extra libraries of course. For example, I tried using a single tabular but the hlines extend to the first column which is not what I want :

错位的嵌套表格在另一个表格内部。

It might be a simple solution I don't know for this without using multirow.

Here is a minimal reproducible example for both tabular and nested tabulars that is not aligned :

\documentclass[12pt]{standalone}
\begin{document}
\begin{tabular}{l|ccccc||c|}
    \hline
    $t_6$    &    0    &    0    &    0    &    -1    &    0    &    0
    \\
    \hline
    $t_5$    &    0    &    0    &    0    &    0    &    -1    &    0
    \\
    \hline
    $t_4$    &    0    &    0    &    0    &    0    &    0    &    0
    \\
    \hline
    $t_3$    &    0    &    0    &    0    &    0    &    1    &    0
    \\
    \hline
    $t_2$    &    0    &    0    &    0    &    1    &    0    &    0
    \\
    \hline
    $t_1$    &    0    &    0    &    1    &    0    &    0    &    0
    \\
    \hline
    $t_0$    &    0    &    1    &    0    &    0    &    0    &    0
    \\
    \hline
\end{tabular}
\end{document}
\documentclass[12pt]{standalone}
\begin{document}
\begin{tabular}{l cccccc}
    $t_6$    &
    \begin{tabular}{|ccccc||c|}
    \hline
    0    &    0    &    0    &    -1    &    0    &    0
    \\
    \hline
    \end{tabular}
    \\
    $t_5$    &
    \begin{tabular}{|ccccc||c|}
    \hline
    0    &    0    &    0    &    0    &    -1    &    0
    \\
    \hline
    \end{tabular}
    \\
    $t_4$    &
    \begin{tabular}{|ccccc||c|}
    \hline
    0    &    0    &    0    &    0    &    0    &    0
    \\
    \hline
    \end{tabular}
    \\
    $t_3$    &
    \begin{tabular}{|ccccc||c|}
    \hline
    0    &    0    &    0    &    0    &    1    &    0
    \\
    \hline
    \end{tabular}
    \\
    $t_2$    &
    \begin{tabular}{|ccccc||c|}
    \hline
    0    &    0    &    0    &    1    &    0    &    0
    \\
    \hline
    \end{tabular}
    \\
    $t_1$    &
    \begin{tabular}{|ccccc||c|}
    \hline
    0    &    0    &    1    &    0    &    0    &    0
    \\
    \hline
    \end{tabular}
    \\
    $t_0$    &
    \begin{tabular}{|ccccc||c|}
    \hline
    0    &    1    &    0    &    0    &    0    &    0
    \\
    \hline
    \end{tabular}
\end{tabular}
\end{document}

If it is not possible or way too hard to solve this problem and obtain a well aligned table like the second one (who is well aligned only because every cells have the same amount of characters) without extra library I won't insist. I just think it is good practice to avoid loading libraries if I can.

英文:

I want to make a table where the hlines start to the second column. In order to do so, I nested tabulars but they don't align well as you can see :

错位的嵌套表格在另一个表格内部。

Is it possible to solve this alignment problem with nested tabulars ? I'd prefer not to use extra libraries unless the code becomes awful to manage.

I'm open to other solutions without extra libraries of course. For example, I tried using a single tabular but the hlines extend to the first column which is not what I want :

错位的嵌套表格在另一个表格内部。

It might be a simple solution I don't know for this without using multirow.

Here is a minimal reproducible example for both tabular and nested tabulars that is not aligned :

\documentclass[12pt]{standalone}
\begin{document}
\begin{tabular}{l|ccccc||c|}
	\hline
	$t_6$	&	0	&	0	&	0	&	-1	&	0	&	0
	\\
	\hline
	$t_5$	&	0	&	0	&	0	&	0	&	-1	&	0
	\\
	\hline
	$t_4$	&	0	&	0	&	0	&	0	&	0	&	0
	\\
	\hline
	$t_3$	&	0	&	0	&	0	&	0	&	1	&	0
	\\
	\hline
	$t_2$	&	0	&	0	&	0	&	1	&	0	&	0
	\\
	\hline
	$t_1$	&	0	&	0	&	1	&	0	&	0	&	0
	\\
	\hline
	$t_0$	&	0	&	1	&	0	&	0	&	0	&	0
	\\
	\hline
\end{tabular}
\end{document}
\documentclass[12pt]{standalone}
\begin{document}
\begin{tabular}{l cccccc}
	$t_6$	&
	\begin{tabular}{|ccccc||c|}
	\hline
	0	&	0	&	0	&	-1	&	0	&	0
	\\
	\hline
	\end{tabular}
	\\
	$t_5$	&
	\begin{tabular}{|ccccc||c|}
	\hline
	0	&	0	&	0	&	0	&	-1	&	0
	\\
	\hline
	\end{tabular}
	\\
	$t_4$	&
	\begin{tabular}{|ccccc||c|}
	\hline
	0	&	0	&	0	&	0	&	0	&	0
	\\
	\hline
	\end{tabular}
	\\
	$t_3$	&
	\begin{tabular}{|ccccc||c|}
	\hline
	0	&	0	&	0	&	0	&	1	&	0
	\\
	\hline
	\end{tabular}
	\\
	$t_2$	&
	\begin{tabular}{|ccccc||c|}
	\hline
	0	&	0	&	0	&	1	&	0	&	0
	\\
	\hline
	\end{tabular}
	\\
	$t_1$	&
	\begin{tabular}{|ccccc||c|}
	\hline
	0	&	0	&	1	&	0	&	0	&	0
	\\
	\hline
	\end{tabular}
	\\
	$t_0$	&
	\begin{tabular}{|ccccc||c|}
	\hline
	0	&	1	&	0	&	0	&	0	&	0
	\\
	\hline
	\end{tabular}
\end{tabular}
\end{document}

If it is not possible or way too hard to solve this problem and obtain a well aligned table like the second one (who is well aligned only because every cells have the same amount of characters) without extra library I won't insist. I just think it is good practice to avoid loading libraries if I can.

答案1

得分: 1

在这些文字中,我读到你希望事情变得简单(我们都希望如此吧?),因为你不想包含任何额外的包。总的来说,这不是事情通常的工作方式。LaTeX中的基本类别通过使用包来进行扩展 - 不要让这成为一个担忧;在许多情况下,它们使使用变得更简单。

此外,你的对齐问题评估是正确的,但可以通过使用一些基本的tabular构造来纠正。

下面我添加了array - 它是每个TeX发行版的一部分 - 允许一些扩展的列规范。特别是,w{<align>}{<width>}可以设置在固定宽度列中的对齐方式<align>。例如,wc{2em}将使其内容居中在一个2em宽的列内。另一种方法是使用>{\centering\arraybackslash}p{2em},但这也需要使用array。如果你真的不想使用array,你可以只使用p{2em},然后在每个单元格中添加\centering...但这样复制会使代码更难维护。

在上面的代码中,你将看到一个名为\myvector{<label>}[<# elements>]{<elements>}的宏,它接受两个必需参数:<label>设置在向量左侧的固定宽度框中,宽度为2em,以及使用常规tabular样式规范(element & element & ...)的向量的<elements>。第二个参数<# elements>是可选的,默认为5 - 最后一个元素/列之前的元素数量。如果你想要一个更大/更小的向量,可以使用该参数来调整显示,例如:

\myvector{X}[3]{1 & 2 & 3 & 4}

\myvector{Y}[7]{1 & 2 & 3 & 4 & 5 & 6 & 7 & 8}
英文:

Between the lines I read that you want things to be easy (don't we all?) because you don't want to include any additional packages. This is really not how things work, in general. The basic classes in LaTeX are extended by the use of packages - don't let that be a concern; in many cases they make things simpler to use.

Also, your alignment issue assessment is correct, but can be corrected using some basic tabular constructions.

Below I've added the array package - part of every TeX distribution - which allows for some extended column specifications. In particular, w{&lt;align&gt;}{&lt;width&gt;} that sets the &lt;align&gt;ment within a fixed-&lt;width&gt; column. For example, wc{2em} will center its contents within a 2em-width column. The alternative is to use &gt;{\centering\arraybackslash}p{2em}, but this also requires array. If you really don't want to use array, you can just use p{2em} and then issue \centering in every cell... but such replication makes the code harder to maintain.

错位的嵌套表格在另一个表格内部。

\documentclass{article}

\usepackage{array}

\newlength{\cellwidth}
\setlength{\cellwidth}{1em}
\NewDocumentCommand{\myvector}{ m O{5} m }{%
  \makebox[2em][l]{#1}%
  $\begin{array}{ | *{#2}{wc{\cellwidth}} || c | }
    \hline
    #3 \\
    \hline
  \end{array}$%
}

\begin{document}

\begin{tabular}{l}
  \myvector{$t_6$}{0 &amp; 0 &amp; 0 &amp; -1 &amp;  0 &amp; 0} \\
  \myvector{$t_5$}{0 &amp; 0 &amp; 0 &amp;  0 &amp; -1 &amp; 0} \\
  \myvector{$t_4$}{0 &amp; 0 &amp; 0 &amp;  0 &amp;  0 &amp; 0} \\
  \myvector{$t_3$}{0 &amp; 0 &amp; 0 &amp;  0 &amp;  1 &amp; 0} \\
  \myvector{$t_2$}{0 &amp; 0 &amp; 0 &amp;  1 &amp;  0 &amp; 0} \\
  \myvector{$t_1$}{0 &amp; 0 &amp; 1 &amp;  0 &amp;  0 &amp; 0} \\
  \myvector{$t_0$}{0 &amp; 1 &amp; 0 &amp;  0 &amp;  0 &amp; 0}
\end{tabular}

\end{document}

What you'll see in the above code is a macro called \myvector{&lt;label&gt;}[&lt;# elements&gt;]{&lt;elements&gt;} that takes 2 mandatory arguments: &lt;label&gt; set to the left of the vector in a left-aligned fixed width box of width 2em, and the &lt;elements&gt; of that vector using the regular tabular-like specification (element &amp; element &amp; ...). The second argument &lt;# elements&gt; is optional, and defaults to 5 - the number of elements before the final element/column. If you want a larger/smaller vector, you can use that argument to adjust the display, as in

\myvector{X}[3]{1 &amp; 2 &amp; 3 &amp; 4}

or

\myvector{Y}[7]{1 &amp; 2 &amp; 3 &amp; 4 &amp; 5 &amp; 6 &amp; 7 &amp; 8}

huangapple
  • 本文由 发表于 2023年5月25日 01:15:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76325962.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定