Follow-up from this thread.
Issue
phantom seems to have an inconsistent behaviour depending on whether something is printed before its call when used in a p column type in a table.
- When used on its own, it generates a linebreak;
- When highlighted with
colorboxit fixes itself but generates 2 extra characters (spaces); - When prefixed by a negative
hspace, it works properly but this does not seem right; - When used with both
colorboxandhspace, another extra space appears.
Bonus question
In my newcolumntype definition, I use textwidth to define the width of the columns but this does not take into account tabcolsep. Is there a way to somehow redefine it to be \textwidth-\tabcolsep?
Demo
MWE
\documentclass{article}
\usepackage{mwe}
\usepackage{booktabs}
\usepackage{array}
\usepackage{hyperref}
\usepackage{xcolor}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1\textwidth}}
\newcommand{\mail}[1]{
\href{mailto:#1}{#1}
}
\begin{document}
\centering
\noindent
\begin{tabular}{L{.03}|L{.5}}
\toprule
\textbf{\#} & \textbf{w/ phantom} \\
\midrule
1 & {\ttfamily\phantom{xxx}\mail{a@tex.c}} \\
2 & {\ttfamily\phantom{xx}\mail{ab@tex.co}} \\
3 & {\ttfamily\mail{abcd@tex.com}} \\
\bottomrule
\end{tabular}
\noindent
\begin{tabular}{L{.03}|L{.5}}
\toprule
\textbf{\#} & \textbf{w/ colorbox} \\
\midrule
1 & {\ttfamily\colorbox{red}{\phantom{xxx}}\mail{a@tex.c}} \\
2 & {\ttfamily\colorbox{red}{\phantom{xx}}\mail{ab@tex.co}} \\
3 & {\ttfamily\mail{abcd@tex.com}} \\
\bottomrule
\end{tabular}
\noindent
\begin{tabular}{L{.03}|L{.5}}
\toprule
\textbf{\#} & \textbf{w/ hspace} \\
\midrule
1 & {\ttfamily\hspace{-.5em}\phantom{xxx}\mail{a@tex.c}} \\
2 & {\ttfamily\hspace{-.5em}\phantom{xx}\mail{ab@tex.co}} \\
3 & {\ttfamily\hspace{-.5em}\mail{abcd@tex.com}} \\
\bottomrule
\end{tabular}
\noindent
\begin{tabular}{L{.03}|L{.5}}
\toprule
\textbf{\#} & \textbf{w/ hspace \& colorbox} \\
\midrule
1 & {\ttfamily\hspace{-.5em}\colorbox{red}{\phantom{xxx}}\mail{a@tex.c}} \\
2 & {\ttfamily\hspace{-.5em}\colorbox{red}{\phantom{xx}}\mail{ab@tex.co}} \\
3 & {\ttfamily\hspace{-.5em}\mail{abcd@tex.com}} \\
\bottomrule
\end{tabular}
\end{document}




\leavevmode\phantom{xxx}– egreg Sep 27 '17 at 10:52\hbox{zzz}as opposed to\mbox{zzz}If you use low level commands in a latex context then you have to take care of more primitive tex behaviour. – David Carlisle Sep 27 '17 at 12:14