11

Minimal working example:

\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}

How can I define a similar command to \textcolor, say \mytextcolor so that

This is \mytextcolor{red}{red color} rest of the line

produces this (puts bold 1 and 2 between colored text):

enter image description here

If the colored text spans more than one lines like this:

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

Puts bold 1 and bold 2 like this:

enter image description here

and if the colored text even spans more lines, then the output would look like this:

enter image description here

Note: Using tikz or any other thing which uses \special is simply not wanted.

  • Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code. – cfr May 8 '15 at 3:29
  • You've tagged this as tex-core but you appear to be using LaTeX. Could you clarify? tex-core is for questions about things which don't use any format (e.g. LaTeX). Also, note that TikZ does not use \special - not typically, anyhow. Are you open to any other possible end results? I leave this to the TeX gurus, but I ask because I suspect that what you are asking is in the Extremely-Difficult-Any-Solution-Will-Break-All-Kinds-of-Stuff category, which is best avoided if possible. – cfr May 8 '15 at 3:33
  • 2
    You cannot avoid \special (or \pdfliteral) when you need colors. – wipet May 8 '15 at 3:39
  • @wipet: That is ok but using tikz or \special trick for detecting end of line is not ok. – user77791 May 8 '15 at 3:42
  • @wipet I was guessing the aversion was tied to a preference for using pdfLaTeX rather than LaTeX but I could be wrong. – cfr May 8 '15 at 3:42
9

I can show you the plain TeX solution. You can re-write this to your used macro package as a homework.

\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

semph

  • Perfect. You gave me a solution to fix an TeX--XeT bug which existed for years. I have no idea how to thank you. – user77791 May 8 '15 at 4:25
  • But this suppresses hyphenation, right? is there any other way to not effect hyphenation? – user77791 May 8 '15 at 10:34
  • @user77791 Yes, the hyphenation is suppressed. I did a little correction of the code now (first \discretionary, second \hskip). If you insert \nobreak immediately before \discretionary then hyphenation is allowed, but the lines with hyphenated words don't end by {\bf2}, they end only by hyphen. – wipet May 8 '15 at 15:45
  • @user77791 You mentioned that you need to fix a bug in TeX--XeT. IMHO, the right place where to solve such problem is source code of TeX--XeT, no \discretionary primitive. – wipet May 8 '15 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.