4

I wanted to highlight part of my sequence. The problem is that \fcolorbox shifts the text so that the sequences are no longer aligned.

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

enter image description here

How can I remove the space inserted by the \fcolorbox ?

PS: \makebox() did not help.

Roland
  • 6,655

2 Answers2

4

You can remove the impact of the space and rule inserted around the box:

enter image description here

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


You can also set the \fcolorbox in the background by capturing the position of the text (using zref's savepos module) and placing a \phantom element via eso-pic.

enter image description here

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

Werner
  • 603,163
3

My answer is inspired to this one.

With this code (where I changed the color for more readability):

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

You have the desired result: enter image description here