最小工作示例:
\documentclass{article}
\usepackage{color}
\newcommand{\mytextcolor}[2]{\textbf{1}\textcolor{#1}{#2}\textbf{2}}
\begin{document}
This is \mytextcolor{red}{red color} rest of the line
\end{document}
我怎样才能定义类似的命令\textcolor
,\mytextcolor
比如说
This is \mytextcolor{red}{red color} rest of the line
产生如下结果(将粗体 1 和 2 放在彩色文本之间):
如果彩色文本跨越多行,如下所示:
This is a test as we can see so the next thing \mytextcolor{red}{red color so we can do this here and there} rest of the line
像这样放置粗体 1 和粗体 2:
如果彩色文本跨越更多行,则输出将如下所示:
笔记:使用 tikz 或任何其他东西\special
都是根本不想要的。
答案1
我可以向你展示纯 TeX 解决方案。你可以将其重写到你使用的宏包中作为家庭作业。
\input opmac
\localcolor
\newbox\semphAbox \newbox\semphBbox
\setbox\semphAbox=\hbox{{\Black\bf1}}
\setbox\semphBbox=\hbox{{\Black\bf2}}%
\def\semph#1{\def\tmpb{#1}\replacestrings{ }{\specialspace}%
{\Red\copy\semphAbox\tmpb\copy\semphBbox}%
}
\def\specialspace{%
\discretionary{\copy\semphBbox}{\copy\semphAbox}{\kern\fontdimen2\font}%
\nobreak\hskip 0pt plus\fontdimen3\font minus\fontdimen4\font
}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
\semph{Ut purus elit, vestibulum ut, placerat ac,
adipiscing vitae, felis. Curabitur dictum gravida mauris. Nam arcu
libero, nonummy eget, consectetuer id, vulputate a, magna.}
Donec vehicula augue eu neque.
\semph{Pellentesque habitant} morbi tristique senectus et netus et malesuada
fames ac turpis egestas. Mauris ut leo. Cras viverra metus rhoncus sem.
Nulla et lectus vestibulum urna fringilla ultrices.
\bye