如何在不使用包包的情況下在 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
將使用更複雜的巨集來提供調整交叉線、下劃線等的方法...