我已经尝试过两个类似问题中给出的解决方案:表格中的 TikZ 图片和Tikz 表格单元格内但它们似乎对我不起作用。
下面我给出了 MWE 和我尝试过的方法。我在一个长表格单元格内有一张 tikz 图片。我希望它自动适应单元格的宽度。
使用实际图像时,\includegraphics
我会手动调整width
以使其适合。但不知道如何对 tikz 图片进行调整。
我还为单元格使用了“m”选项,以使图像与其他单元格中的文本对齐。但是,如果我删除“m”选项并在该单元格上放回“p”选项,则 tikz 缩放比例没有任何变化。
平均能量损失
\documentclass[11pt]{article}
\usepackage{amsmath,mathtools,amssymb}
\ifdefined\HCode %Michal's driver for tex4ht use
\def\pgfsysdriver{pgfsys-dvisvgm4ht.def}
\fi
\usepackage{tikz,graphicx,tikz-dependency}
\usetikzlibrary{fit}
\usepackage{longtable}
\usepackage{array} %for the m option on cells
\begin{document}
\begin{longtable}[c]{|p{.25in}|p{1in}|m{1.4in}|p{0.5in}|p{0.35in}|p{0.4in}|p{0.35in}|p{.2in}|}\hline
A&
Heat PDE in bar (1D)&
\begin{tikzpicture}[baseline=(current bounding box.center)]
\coordinate[label =above:$0$, label =below:{$u=0$}] (A) at (0,0);
\coordinate[label =above:$\pi$, label =below:{$u_x=0$}] (B) at (4,0);
\draw (A) -- node[above] {$u_t = k u_{xx} $} node[below] {$f(x)$} ++ (B);
\node at (A)[circle,fill,inner sep=1pt]{};
\node at (B)[circle,fill,inner sep=1pt]{};
%\addvmargin{1mm} %has no effect if I uncomment it
\end{tikzpicture}
(Haberman 2.3.3 (a))&
B&0.415&C&2.376&Yes\\\hline
\end{longtable}
\end{document}
如何缩放 tikz 图片尺寸以自动适应表格单元格?
lualatex foo2.tex
在 Linux 上使用 TL 2019 进行编译。
lualatex foo2.tex
This is LuaTeX, Version 1.10.0 (TeX Live 2019)
restricted system commands enabled.
答案1
我猜这肯定是某种重复的答案,但无论如何,adjustbox
它都是为这样的事情而做的。请注意,如果您这样做,则不应使用remember picture
并尝试从“外部”访问坐标。并且\adjustbox
带有自己的垂直或水平对齐机制,但不使用任何机制似乎可以正常工作。无论如何,您也不应该期望baseline=(current bounding box.center)
在这里正常工作。在某种程度上,它将tikzpicture
表现为包含在中的图形\includegraphics
,但如果这就是您想要的,那么adjustbox
似乎是正确的工具。
\documentclass[11pt]{article}
\usepackage{amsmath,mathtools,amssymb}
\ifdefined\HCode %Michal's driver for tex4ht use
\def\pgfsysdriver{pgfsys-dvisvgm4ht.def}
\fi
\usepackage{tikz,tikz-dependency}
\usetikzlibrary{fit}
\usepackage{longtable}
\usepackage{array} %for the m option on cells
\usepackage{adjustbox}
\begin{document}
\begin{longtable}[c]{|p{.25in}|p{1in}|m{1.4in}|p{0.5in}|p{0.35in}|p{0.4in}|p{0.35in}|p{.2in}|}\hline
A&
Heat PDE in bar (1D)&
\adjustbox{max width=1.2in}{\begin{tikzpicture}
\coordinate[label =above:$0$, label =below:{$u=0$}] (A) at (0,0);
\coordinate[label =above:$\pi$, label =below:{$u_x=0$}] (B) at (0.95in,0);
\draw (A) -- node[above] {$u_t = k u_{xx} $} node[below] {$f(x)$} ++ (B);
\node at (A)[circle,fill,inner sep=1pt]{};
\node at (B)[circle,fill,inner sep=1pt]{};
%\addvmargin{1mm} %has no effect if I uncomment it
\end{tikzpicture}}
(Haberman 2.3.3 (a))&
B&0.415&C&2.376&Yes\\\hline
\end{longtable}
\end{document}
附录:上文提到的您可以重写 ,tikzpicture
使其具有所需的宽度。但是,这不是一个通用的解决方案。我添加它只是为了说明不需要引入像 这样的临时距离0.6\linewidth
,但您必须查看图片并找出最左边和最右边的节点(在本例中u=0
为 和u_x=0
)。然后可以从这些“极值节点”开始,使用适当的锚点(以及outer sep=0pt
)将它们放置在图片具有正确宽度的位置,然后添加相对于它们的其余部分。
\documentclass[11pt]{article}
\usepackage{amsmath,mathtools,amssymb}
\ifdefined\HCode %Michal's driver for tex4ht use
\def\pgfsysdriver{pgfsys-dvisvgm4ht.def}
\fi
\usepackage{tikz,tikz-dependency}
\usetikzlibrary{fit}
\usepackage{longtable}
\usepackage{array} %for the m option on cells
\usepackage{adjustbox}
\begin{document}
\begin{longtable}[c]{|p{.25in}|p{1in}|m{1.4in}|p{0.5in}|p{0.35in}|p{0.4in}|p{0.35in}|p{.2in}|}\hline
A&
Heat PDE in bar (1D)&
\begin{tikzpicture}[baseline=(current bounding box.center),nodes={outer xsep=0pt}]
\node[anchor=west] (A0) at (0,0) {$u=0$};
\node[anchor=east] (B0) at (\linewidth,0) {$u_x=0$};
\node[label=above:$0$] (A) at ([yshift=0.2ex]A0.north) [circle,fill,inner sep=1pt]{};
\node[label =above:$\pi$] (B) at (B0|-A) [circle,fill,inner sep=1pt]{};
\draw (A) -- node[above] {$u_t = k u_{xx} $} node[below] {$f(x)$} ++ (B);
\end{tikzpicture}
(Haberman 2.3.3 (a))&
B&0.415&C&2.376&Yes\\\hline
\end{longtable}
\end{document}
答案2
使图像宽度(由线长+节点宽度决定)依赖于单元格宽度:
\documentclass[11pt]{article}
\usepackage{geometry}
\usepackage{mathtools,amssymb}
\ifdefined\HCode %Michal's driver for tex4ht use
\def\pgfsysdriver{pgfsys-dvisvgm4ht.def}
\fi
\usepackage{tikz,graphicx,tikz-dependency}
\usetikzlibrary{fit,
positioning,
quotes}
\usepackage{array, longtable}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
\begin{document}
\begingroup
\begin{longtable}{|m{0.25in}|m{1in}|m{1.5in}|>{\centering\arraybackslash}p{0.5in}
|m{0.35in}|m{0.35in}|m{0.35in}|m{.25in}|}\hline
A & Heat PDE in bar (1D)
& \begin{tikzpicture}[baseline=(current bounding box.base),
every node/.append style = {font=\footnotesize},
dot/.style = {circle, fill, inner sep=2pt}
]
\node (A) [dot,label=$0$, label=below:{$u=0$}] {};
\node (B) [dot,label=$\pi$, label=below:{$u_x=0$},
right=0.6\linewidth of A] {}; % <---
\draw (A) to ["$u_t=k u_{xx}$","$f(x)$" '](B);
\end{tikzpicture}
(Haberman 2.3.3 (a))
& B & 0.415 & C & 2.376 & Yes \\\
hline
\end{longtable}
\endgroup
\end{document}