![使用 \fcolorbox 突出显示文本,不带空格](https://linux22.com/image/441854/%E4%BD%BF%E7%94%A8%20%5Cfcolorbox%20%E7%AA%81%E5%87%BA%E6%98%BE%E7%A4%BA%E6%96%87%E6%9C%AC%EF%BC%8C%E4%B8%8D%E5%B8%A6%E7%A9%BA%E6%A0%BC.png)
我想突出显示序列的一部分。问题是这\fcolorbox
会移动文本,导致序列不再对齐。
\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\newcommand{\cbox}[1]{\fcolorbox{red!80}{yellow}{#1}}
\begin{document}
\begin{table}[]
% \caption{}
%\label{tab:my-table}
\begin{tabular}{ll}
Seq1 & gactttgtggacatcaacgtcggc\cbox{tgc}cccatcgacctcgtgtac \\
Seq2 & gactttgtggacatcaacgtcggctgccccatcgacctcgtgtac \\
Seq3 & gactttgtggacatcaacgtcggctgccccatcgacctcgtgtac \\
& *****************************************
\end{tabular}
\end{table}
\end{document}
我怎样才能删除插入的空格\fcolorbox
?
PS:\makebox()
没帮助。
答案1
您可以消除在框周围插入的空间和规则的影响:
\documentclass{article}
\usepackage{xcolor,array}
\newcommand{\cbox}[1]{{%
\setlength{\fboxsep}{-\fboxrule}% Remove impact of space and rule inserted by box
\fcolorbox{red!80}{yellow}{\strut#1}%
}}
\begin{document}
\begin{tabular}{ l >{\ttfamily} l }
Seq1 & gactttgtggacatcaacgtcggc\cbox{tgc}cccatcgacctcgtgtac \\
Seq2 & gactttgtggacatcaacgtcggctgccccatcgacctcgtgtac \\
Seq3 & gactttgtggacatcaacgtcggctgccccatcgacctcgtgtac \\
& *********************************************
\end{tabular}
\end{document}
您还可以\fcolorbox
通过捕获文本的位置来设置背景(使用zref
的savepos
模块)并\phantom
通过eso-pic
。
\documentclass{article}
\usepackage{xcolor,array,zref-savepos,eso-pic}
\newcounter{cboxcntr}
\newcommand{\cbox}[1]{{%
\stepcounter{cboxcntr}% Allow for unique \cbox
\zsavepos{cbox-\thecboxcntr}% Capture (x,y) coordinate of cbox
#1%
\begingroup
\edef\x{\endgroup\noexpand\AddToShipoutPictureBG*{%
\noexpand\AtPageLowerLeft{%
\noexpand\setlength{\noexpand\fboxsep}{.5\noexpand\fboxsep}% Reduce the fbox padding
\noexpand\hspace*{\dimexpr\zposx{cbox-\thecboxcntr}sp-\fboxsep-\fboxrule}%
\noexpand\raisebox{\zposy{cbox-\thecboxcntr}sp}{%
\noexpand\fcolorbox{red!80}{yellow}{\noexpand\phantom{\noexpand\ttfamily\noexpand\strut#1}}%
}%
}%
}}\x%
}}
\begin{document}
\begin{tabular}{ l >{\ttfamily} l }
Seq1 & gactttgtggacatcaacgtcggc\cbox{tgc}cccatcgacctcgtgtac \\
Seq2 & gactttgtgg\cbox{aca}tcaacgtcggctgccccatcgacctcgtgtac \\
Seq3 & gactttgtggacatcaacgtcggctgccccatcgac\cbox{ctcg}tgtac \\
& *********************************************
\end{tabular}
\end{document}
答案2
我的回答受到启发这个。
使用此代码(我改变了颜色以提高可读性):
\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{soul}
\newcommand{\hlc}[2][yellow]{{%
\colorlet{foo}{#1}%
\sethlcolor{foo}\hl{#2}}%
}
\newcommand{\mybox}[1]{\hlc[red!40!yellow]{#1}}
\begin{document}
\begin{table}[]
% \caption{}
%\label{tab:my-table}
\begin{tabular}{ll}
Seq1 & gactttgtggacatcaacgtcggc\mybox{tgc}cccatcgacctcgtgtac \\
Seq2 & gactttgtggacatcaacgtcggctgccccatcgacctcgtgtac \\
Seq3 & gactttgtggacatcaacgtcggctgccccatcgacctcgtgtac \\
& *****************************************
\end{tabular}
\end{table}
\end{document}