如何在不使用包的情况下在 Latex 中划掉句子中的单词?
梅威瑟:
\documentclass{article}
\usepackage{cancel}
\begin{document}
%BeginDocument
The first \xcancel{three commands} work in text mode also i.e.,\xcancel{science}
%End Document
\end{document}
答案1
你的教授应该更清楚。在我看来,这根本没教你任何关于 LaTeX 的知识;使用包是道路。
但它就在这里;它包括了欧几里得最大公约数算法的实现(可能太笨拙),以便将一对正确的值传递给\line
。
\documentclass{article}
\makeatletter
\newcommand{\crossout}[1]{%
\begingroup
\settowidth{\dimen@}{#1}%
\setlength{\unitlength}{0.05\dimen@}%
\settoheight{\dimen@}{#1}%
\count@=\dimen@
\divide\count@ by \unitlength
\count0=20 \count4=\count@
\loop
\count2=\count0 % keep a copy
\divide\count2\count4 \multiply\count2\count4
\ifnum\count2<\count0
\advance\count0 -\count2 % the remainder
\count2=\count0
\count0=\count4
\count4=\count2
\repeat
\count0=20 \divide\count0\count4
\count2=\count@ \divide\count2\count4
\begin{picture}(0,0)
\put(0,0){\line(\count0,\count2){20}}
\put(0,\count@){\line(\count0,-\count2){20}}
\end{picture}%
#1%
\endgroup
}
\makeatother
\begin{document}
This word is \crossout{crossed} out
\end{document}
当然,提出这个解决方案是作弊。而且,由于 接受的对有严格的限制,它不会在所有情况下都有效\line
。你的教授一定知道 TeX 不会画斜线。
随着标准打包pict2e
更容易,并且在任何情况下都可以工作。
\documentclass{article}
\usepackage{pict2e}
\makeatletter
\newcommand{\crossout}[1]{%
\begingroup
\settowidth{\dimen@}{#1}%
\setlength{\unitlength}{0.05\dimen@}%
\settoheight{\dimen@}{#1}%
\count@=\dimen@
\divide\count@ by \unitlength
\begin{picture}(0,0)
\put(0,0){\line(20,\count@){20}}
\put(0,\count@){\line(20,-\count@){20}}
\end{picture}%
#1%
\endgroup
}
\makeatother
\begin{document}
This word is \crossout{crossed} out
\crossout{word}
\crossout{U}
\end{document}
不同的解决方案(仅限 pdftex 或 luatex)
\documentclass{article}
\makeatletter
\newcommand{\crossout}[1]{%
\begingroup
\sbox\z@{#1}%
\dimen\z@=\wd\z@
\dimen\tw@=\ht\z@
\dimen\z@=.99626\dimen\z@ % get big points
\dimen\tw@=.99626\dimen\tw@ % get big points
\edef\co@wd{\strip@pt\dimen\z@}% just the number
\edef\co@ht{\strip@pt\dimen\tw@}% just the number
\leavevmode
\rlap{\pdfliteral{q 1 J 0.4 w 0 0 m \co@wd\space \co@ht\space l S Q}}%
\rlap{\pdfliteral{q 1 J 0.4 w 0 \co@ht\space m \co@wd\space 0 l S Q}}%
#1%
\endgroup
}
\makeatother
\begin{document}
This is \crossout{crossed} out
\end{document}
答案2
你的教授希望你学习 TeX,他是一个聪明人!试试下面的代码。
\documentclass{article}
\usepackage{xcolor}
\makeatletter
\newbox\@tempboxb
\def\cancel#1{%
\leavevmode
\setbox\@tempboxa\hbox{#1}
\setbox\@tempboxb\hbox{x}
\hbox to 0pt{\hbox to \wd\@tempboxa {\color{red}\leaders\copy\@tempboxb\hfill\kern0pt}}#1}
\makeatother
\begin{document}
This is \cancel{a very long word}
\end{document}
方法是将单词放在一个框中并测量宽度。然后在其上绘制一条规则(使用引线,您可以使用任何符号,例如x
或-
)。诸如 之类的包ulem
将使用更复杂的宏来提供调整交叉线、下划线等的方法...