2
\documentclass[a4paper]{article}
\usepackage[most]{tcolorbox}
\usepackage{xeCJK}
\setCJKmainfont{SimSun}

\begin{document}

% Code Block I %
MM%
\begin{tcolorbox}[blankest, nobeforeafter,bottom=0pt,boxsep=0pt, opacitytext=0.2]
  \color{red}MM
\end{tcolorbox}%
MM\\
%%%%%%%%%%%%%%%%%%%%%%%%%%

% Code Block II %
MM%
\begin{tcolorbox}[blankest, nobeforeafter,bottom=0pt,boxsep=0pt, opacitytext=0.2]
  \color{red}地方
\end{tcolorbox}%

\end{document}

From the "code block I", I want to get six continuous characters "M". However, the typeset gives unwanted whitespace. I think this is because the width of tcolorbox is default of \textwidth. How can I get a tcolorbox which width changes with its content?

From the typeset of "code block II" which contains non-ascii characters(here is CJK,for example), we can see that there is unalignment in height between characters. How to align ascii characters with CJK characters?

PS: I know \tcbox may be a soluntion. But I want line-break sometimes. So tcolorbox must be choosed.

enter image description here

lyl
  • 2,727
  • tcbox adjust their width to thier contents, but tcolorbox uses linewidth as their defult width, unless you use width option. – Ignasi May 03 '18 at 10:32
  • In any case, your code doesn't compile,[most] option to package tcolorbox is missing. And my system complains about CJK characters. – Ignasi May 03 '18 at 10:33
  • Thanks @lgnasi, do you have any idea how to break lines in tcbox? – lyl May 03 '18 at 10:35
  • For box alignment use baseline or box align options (page 79) – Ignasi May 03 '18 at 10:35
  • Something like this? https://tex.stackexchange.com/questions/125162/rounded-box-around-placeholder-text-that-supports-line-breaking – Steven B. Segletes May 03 '18 at 10:36
  • parbox or tabulars can be used inside tcbox – Ignasi May 03 '18 at 10:39
  • @lgnasi: Does that mean every time I need to try for a new value(fontsize changes, for example)? Is there a common method to keep all charactors in the same baseline? – lyl May 03 '18 at 10:44
  • For the problem of break-line, actually I need a way to achieve auto-break-line (when the content of text reach the page margin. – lyl May 03 '18 at 10:49
  • @lgnasi "In any case, your code doesn't compile,...". Thank you for your reminder. I just modified my code. – lyl May 03 '18 at 11:00
  • Why do you use tcolorbox at all? Wouldn't \textcolor{red}{MM} be enough? – Ulrike Fischer May 03 '18 at 13:06
  • Because tcolorbox provide with the feature of "opacity". I wonder if there are better ways to get text opcacity effect. – lyl May 03 '18 at 13:19

1 Answers1

4

You don't need tcolorbox (which creates boxes and so make line breaking difficult) only to get colored, transparent text.

\documentclass[a4paper]{article}
\usepackage{pgf,xcolor}

\begin{document}

MM%
\textcolor{red}{MM}%
\textcolor{red}{\pgfsetfillopacity{0.5}MM\pgfsetfillopacity{1}}
MM

\end{document}

opacity settings don't respect tex groups, so you must reset them explictly.

enter image description here

Ulrike Fischer
  • 327,261