![如何避免或补偿 tikz 删除节点中的水平和垂直偏移?](https://linux22.com/image/371566/%E5%A6%82%E4%BD%95%E9%81%BF%E5%85%8D%E6%88%96%E8%A1%A5%E5%81%BF%20tikz%20%E5%88%A0%E9%99%A4%E8%8A%82%E7%82%B9%E4%B8%AD%E7%9A%84%E6%B0%B4%E5%B9%B3%E5%92%8C%E5%9E%82%E7%9B%B4%E5%81%8F%E7%A7%BB%EF%BC%9F.png)
下面的 MCE 指出,对于tikz
' 删除节点, line width
会导致水平和垂直移动。是否有可能
- 避免它们?
- 或者,至少,要准确地知道这些转变的程度,以便对其进行补偿(删除的文本与“正常”文本位于同一位置)?
这个问题与如何避免“删除”内容的转变(来自 tikz shapes.misc 库)?但反过来:在这里,我希望“正常”文本不是被删除的文本将被移动,但被删除的文本将被不是被转移并且处于与“正常”位置相同的位置。
\documentclass{article}
\usepackage[arrows=false]{pagegrid}
\usepackage[margin=0cm,papersize=2cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{shapes}
%
\setlength{\parindent}{0pt}
%
\newcommand{\test}[1][]{%
\tikz \node [draw,inner sep=0pt,outer sep=0pt,strike out,draw=red,#1] {%
test%
};%
}
%
\begin{document}
test \test{} \test[line width=.5ex]
\test{} \test[line width=.5ex] test
\test[line width=.5ex] test \test{}
\end{document}
答案1
如果我理解正确的话,您也许可以将打击绘制为一个单独的节点(使用库fit
来获取正确的大小),并将overlay
选项添加到该节点,这样它就不会被考虑用于边界框的计算。
\documentclass{article}
\usepackage[arrows=false]{pagegrid}
\usepackage[margin=0cm,papersize=2cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{shapes,fit} % <-- added fit
%
\setlength{\parindent}{0pt}
%
\newcommand{\test}[1][]{%
\begin{tikzpicture}
% first add text
\node [inner sep=0pt,outer sep=0pt] (foo) {test};
% draw strike out in separate node, with overlay option
\node [overlay,inner sep=0pt,fit=(foo),strike out,draw=red,#1] {};
\end{tikzpicture}%
}
%
\begin{document}
test \test{} \test[line width=.5ex]
\test{} \test[line width=.5ex] test
\test[line width=.5ex] test \test{}
\end{document}
答案2
使用 tikz创建自己的命令非常容易strike out
。只需使用[remember picture,overlay]
tikz 环境的选项即可避免干扰文本。
\documentclass{article}
\usepackage[arrows=false]{pagegrid}
\usepackage[margin=0cm,papersize=2cm]{geometry}
\usepackage{tikz,calc}
\usetikzlibrary{shapes}
%
\setlength{\parindent}{0pt}
%
\newlength\teht
\settoheight\teht{t}
\newcommand\tikzmark[1]{\tikz[remember picture,overlay]\coordinate(#1);}
\newcommand{\test}[2][]{%
\tikzmark{o}#2\tikzmark{a}%
\tikz[remember picture, overlay]{\draw[red,#1](o)--([yshift=\teht]a);}%
}
\begin{document}
test \test{test} \test[line width=.5ex]{test}
\test{test} \test[line width=.5ex]{test} test
\test{test} test \test[line width=.5ex]{test}
\end{document}
答案3
\documentclass{article}
\usepackage[arrows=false]{pagegrid}
\usepackage[margin=0cm,papersize=2cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{tikzpagenodes}
\tikzset{StrikeOut/.style={-,red}}
\makeatletter
\newcommand{\gettikzxy}[3]{% from https://tex.stackexchange.com/a/58590/121799
\tikz@scan@one@point\pgfutil@firstofone#1\relax
\global\edef#2{\the\pgf@x}%
\global\edef#3{\the\pgf@y}%
}
\makeatother
\newcommand{\StrikeOut}[2][]{%
\tikz[remember picture,overlay, baseline=(Begin.base)]{%
\node[anchor=base,inner sep=0pt,outer sep=0pt] (Begin) {\strut};}#2%
\tikz[remember picture,overlay, baseline=(End.base)]{%
\node[anchor=base,inner sep=0pt,outer sep=0pt] (End) {\strut};}%
\begin{tikzpicture}[overlay,remember picture]
\gettikzxy{($(Begin.north)-(current page.south west)$)}{\BeginxN}{\BeginyN}
\gettikzxy{($(End.north)-(current page.south west)$)}{\EndxN}{\EndyN}%\typeout{\BeginyN\space\EndyN}
\pgfmathtruncatemacro{\mytest}{\EndyN-\BeginyN}
\ifnum\mytest=0\relax% begin and and in the same line %\typeout{begin and end in the same line}
\draw[StrikeOut,#1] (Begin.north) -- (End.south);
\draw[StrikeOut,#1] (Begin.south) -- (End.north);
\else% \typeout{end below begin}
\path (current page text area.north west) -- (current page text area.south west)
node(WestLine)[left]{};
\path (current page text area.north east) -- (current page text area.south east)
node(EastLine)[right]{};
\gettikzxy{($(End.north)-(current page.south west)$)}{\EndxN}{\EndyN}
\gettikzxy{($(Begin.south)-(current page.south west)$)}{\BeginxS}{\BeginyS}
\pgfmathtruncatemacro{\mytest}{\BeginyS-\EndyN+1pt}% \typeout{\mytest}
\ifnum\mytest<2\relax% \typeout{end in the next line after begin}%
\pgfmathtruncatemacro{\mytest}{\BeginxS-\EndxN}% \typeout{\mytest}
\ifnum\mytest>0\relax
\draw[StrikeOut,#1] (Begin.north) -- (Begin.south -| EastLine);
\draw[StrikeOut,#1] (Begin.south) -- (Begin.north -| EastLine);
\draw[StrikeOut,#1] (End.north -| WestLine) -- (End.south);
\draw[StrikeOut,#1] (End.south -| WestLine) -- (End.north);
\else
\draw[StrikeOut,#1] (Begin.south) -- (Begin.north -| EastLine);
\draw[StrikeOut,#1] (Begin.north) -- (Begin.south -| EastLine);
\draw[StrikeOut,#1] (End.north -| WestLine) -- (End.south);
\draw[StrikeOut,#1] (End.south -| WestLine) -- (End.north);
\fi
\else
\pgfmathsetmacro{\LineHeight}{\the\baselineskip}
\pgfmathtruncatemacro{\NumLines}{-1+(\BeginyN-\EndyN)/\LineHeight}
\typeout{\NumLines}
\foreach \X in {1,...,\NumLines}
{
\draw[StrikeOut,#1] ($(Begin.north -| WestLine)+(0,-\X*\LineHeight pt)$) --
($(Begin.south -|EastLine)+(0,-\X*\LineHeight pt)$);
\draw[StrikeOut,#1] ($(Begin.south -| WestLine)+(0,-\X*\LineHeight pt)$) --
($(Begin.north -|EastLine)+(0,-\X*\LineHeight pt)$);
}
\draw[StrikeOut,#1] (Begin.north) -- (Begin.south -| EastLine);
\draw[StrikeOut,#1] (Begin.south) -- (Begin.north -| EastLine);
\draw[StrikeOut,#1] (End.north -| WestLine) -- (End.south);
\draw[StrikeOut,#1] (End.south -| WestLine) -- (End.north);
\fi
\fi
\end{tikzpicture}}
\begin{document}
\noindent
test \StrikeOut{test} \StrikeOut[line width=.5ex]{test}\\
\StrikeOut{test} \StrikeOut[line width=.5ex]{test} test\\
\StrikeOut[line width=.5ex]{test} test \StrikeOut{test}\\
\end{document}