我设法创建了三个包含字母的表格:
\begin{minipage}{0.37\textwidth}
\begin{tabular}{c | c | c | c | c}
E & G & P & A & L\\
\hline
I & I & U & G & F\\
\hline
G & A & F & Z & U\\
\hline
\textbf{H} & R & T & U & Z\\
\hline
\textbf{A} & I & N & F & H\\
\hline
\textbf{U} & I & A & C & T\\
\hline
\textbf{S} & N & E & R & Z\\
\end{tabular}
\end{minipage}
\begin{minipage}{0.37\textwidth}
\begin{tabular}{c | c | c | c | c}
E & G & P & A & L\\
\hline
I & I & U & G & F\\
\hline
G & A & F & Z & U\\
\hline
\textbf{H} & \textbf{A} & \textbf{U} & \textbf{S} & Z\\
\hline
L & I & N & F & H\\
\hline
G & I & A & C & T\\
\hline
D & N & E & R & Z\\
\end{tabular}
\end{minipage}
\begin{minipage}{0.37\textwidth}
\begin{tabular}{c | c | c | c | c}
E & G & P & A & L\\
\hline
I & I & U & G & F\\
\hline
G & A & F & Z & U\\
\hline
\textbf{H} & B & R & T & Z\\
\hline
L & \textbf{A} & N & F & H\\
\hline
G & I & \textbf{U} & C & T\\
\hline
D & N & E & \textbf{S} & Z\\
\end{tabular}
\end{minipage}
现在我想在所有粗体字母周围画一个圆圈。这很容易实现吗?
答案1
Torbjørn 引用的答案不处理对角线的情况。要使其工作,您必须相应地旋转矩形。此解决方案的工作原理是放置起点和终点并计算绘制矩形的角度。矩形实际上是一个节点。这是包括示例的代码:
\documentclass{report}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\def\startCirc#1{\tikz[remember picture,overlay]\path node[inner sep=0, anchor=south] (st) {\textbf{#1}} coordinate (start) at (st.center);}%
\def\endCirc#1{\tikz[remember picture,overlay]\path node[inner sep=0, anchor=south] (en) {\textbf{#1}} coordinate (end) at (en.center);%
\begin{tikzpicture}[overlay, remember picture]%
\path (start);%
\pgfgetlastxy{\startx}{\starty}%
\path (end);%
\pgfgetlastxy{\endx}{\endy}%
\pgfmathsetlengthmacro{\xdiff}{\endx-\startx}%
\pgfmathsetlengthmacro{\ydiff}{\endy-\starty}%
\pgfmathtruncatemacro{\xdifft}{\xdiff}%
\pgfmathsetmacro{\xdiffFixed}{ifthenelse(equal(\xdifft,0),1,\xdiff)}%
\pgfmathsetmacro{\angle}{ifthenelse(equal(\xdiffFixed,1),90,atan(\ydiff/\xdiffFixed))}%
\pgfmathsetlengthmacro{\xydiff}{sqrt(abs(\xdiff^2) + abs(\ydiff^2))}%
\path node[draw,rectangle, rounded corners=2mm, dashed, rotate=\angle, minimum width=\xydiff+4ex, minimum height=2.5ex] at ($(start)!.5!(end)$) {};%
\end{tikzpicture}%
}
\noindent
\begin{tabular}{c | c | c | c | c}
E & G & P & A & L\\
\hline
I & I & U & G & F\\
\hline
G & A & F & Z & U\\
\hline
\startCirc{H} & R & T & U & Z\\
\hline
\textbf{A} & I & N & F & H\\
\hline
\textbf{U} & I & A & C & T\\
\hline
\endCirc{S} & N & E & R & Z\\
\end{tabular}
\quad
\begin{tabular}{c | c | c | c | c}
E & G & P & A & L\\
\hline
I & I & U & G & F\\
\hline
G & A & F & Z & U\\
\hline
\startCirc{H} & \textbf{A} & \textbf{U} & \endCirc{S} & Z\\
\hline
L & I & N & F & H\\
\hline
G & I & A & C & T\\
\hline
D & N & E & R & Z\\
\end{tabular}
\quad
\begin{tabular}{c | c | c | c | c}
E & G & P & A & L\\
\hline
I & I & U & G & F\\
\hline
G & A & F & Z & U\\
\hline
\startCirc{H} & B & R & T & Z\\
\hline
L & \textbf{A} & N & F & H\\
\hline
G & I & \textbf{U} & C & T\\
\hline
D & N & E & \endCirc{S} & Z\\
\end{tabular}
\quad
\begin{tabular}{c | c | c | c | c}
E & G & P & A & L\\
\hline
I & I & U & G & F\\
\hline
G & A & F & Z & U\\
\hline
H & B & R & T & \startCirc{Z}\\
\hline
L & A & N & \textbf{F} & H\\
\hline
G & I & \textbf{U} & C & T\\
\hline
D & \endCirc{N} & E & S & Z\\
\end{tabular}
\end{document}
abs
在平方上需要,因为出于某种原因,认为等于TikZ
。\x^2
结果\x<0
如下所示:-\y^2
\y=-\x
请注意,这将无法突出显示单个“圆圈”中彼此相邻的多行。
编辑:为了在水平情况下使线更接近一点,您必须node height
对角度进行条件设置。您可以通过将绘制节点的线替换为以下内容来轻松完成此操作:
\pgfmathsetlengthmacro{\myheight}{ifthenelse(equal(\angle,0),1.5ex,2.5ex)}
\path node[draw,rectangle, rounded corners=1mm, dashed, rotate=\angle, minimum width=\xydiff+4ex, minimum height=\myheight] at ($(start)!.5!(end)$) {};%
请注意,我还将这里rounded corners
的改为1 mm
。否则,节点看起来会“错误”。当然,您也可以以完全相同的方式为此创建一个条件,允许您在其他节点上保留rounded corners
的值。2 mm