Tikz \foreach 多条线段

Tikz \foreach 多条线段

我想知道(我知道有)一种使用 Tikz 创建表格的更简单的方法。我目前手动创建每 x 个单位的列。有没有办法使用 foreach 命令来做到这一点?如您所见,这需要很多时间。感谢您的帮助。这部分代码来自一个更大的代码。

\documentclass{report}
\usepackage{pgfplots}
\usepackage{amsmath}
\usepackage{tikz}
\begin{tikzpicture}
\begin{scope}[shift={(0,-12)}]
\draw (0.2,22)--(17.8,22)--(17.8,17.5)--(0.2,17.5)--cycle;
\draw (0.2,22)--(17.8,22)node[midway,sloped,draw,rectangle,fill=red!15]{Example 3};
\path (0.2,21)node[anchor=west]{\small The table above gives values of a function  $f$  at selected values of  $x$ . Which of the following conclusions is supported by};
\path (0.2,20.5)node[anchor=west]{\small the data in the table?};
\draw (3.4,20)--(14.8,20)--(14.8,19)--(3.4,19)--cycle;
\draw (4.666,20)--(4.666,19);
\draw (5.9332,20)--(5.9332,19);
\draw (7.1999,20)--(7.1999,19);
\draw (8.45992,20)--(8.45992,19);
\draw (9.7265,20)--(9.7265,19);
\draw (10.9931,20)--(10.9931,19);
\draw (12.2598,20)--(12.2598,19);
\draw (13.52644,20)--(13.52644,19);
\draw (14.7930,20)--(14.793,19);

\end{scope}
\end{tikzpicture}
\end{document}


答案1

如果你真的想用 Ti 做到这一点Z(我还是不明白,因为你可以把真实的桌子和 Ti 混合在一起Z 图纸,但无论如何),您可以使用matrix of nodes

\documentclass[a4paper, 11pt]{article}
\usepackage{geometry}
\geometry{hmargin=1cm,vmargin=1cm}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{lipsum}

\tikzset{
    mymat/.style={
          matrix of nodes,
          anchor=center,
          nodes=draw,nodes in empty cells,
          minimum height=10mm,
          minimum width=15mm,
          align=center,
          column sep=-\pgflinewidth
          },
    }
    
\begin{document}
 
\lipsum[1]\\

\begin{center}
    \begin{tikzpicture}
    
        \draw (0,0) rectangle (14,3);
        \node[anchor=center,draw,fill=red!15] at (7,3) {Example 3};
        \node[anchor=center,text width=13cm,align=center] at (7,2)
            {\small The table above gives values of a function  $f$  at selected values of  $x$ . Which of the following conclusions is supported by the data in the table?};       
        \matrix[mymat] (m) at (7,0.8)
                {
                 & & & & & & & & \\
                };
    
    \end{tikzpicture}
\end{center}
    
\end{document}

包含节点矩阵的表

答案2

tcolorbox与环境example和“常规”类似的东西tabular

\documentclass[a4paper]{report}
\usepackage[most]{tcolorbox}


\newtcolorbox[auto counter]{myexample}[1][]{
enhanced, sharp corners, coltitle=black, fontupper=\small,
colback=white, attach boxed title to top center={yshift*=-\tcboxedtitleheight/2},
boxed title style={sharp corners, colback=red!30},
title={Example~\thetcbcounter},
#1
}

\begin{document}
\begin{myexample}
\centering
The table above gives values of a function  $f$  at selected values of  $x$. Which of the following conclusions is supported by the data in the table?

\begin{tabular}{*{9}{|p{8mm}}|}
\hline
\rule{0pt}{7mm}&&&&&&&&\\
\hline
\end{tabular}
\end{myexample}

\end{document}

在此处输入图片描述

相关内容