1

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 colorbox it 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 colorbox and hspace, 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

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}
  • 2
    \leavevmode\phantom{xxx} – egreg Sep 27 '17 at 10:52
  • note this is not related to phantom specifically, try with \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
1

New answer (Edited to use hbox instead of mbox as @David Carlisle suggests) Also added \arraystretch fix

This is a way to avoid all the problems of your way and to automate all the process. You have to use the columns as on my code (r@{}>{@}l or similar) and to give in your tabular the commands: \nameMAIL{nameHere} and \MAILon{providerHere}. It already creates what you want without counting spaces that would be bad in result if a number like 1 was inside the mail name. (x is longer than 1 or than l->small L). So, to avoid all this things you are already ok with these commands. But For your example needed a link too. So I used \tikzmark (from here) and created the command LinkAllMails. Just don't forget to set it's counter (numOfmails) to zero before starting a new tabular. Code:

\documentclass{article}

\usepackage{booktabs}
\usepackage{array}
\usepackage{hyperref}

\usepackage{tikz}
\usetikzlibrary{calc}


\makeatletter
\xdef\myfsize{\f@size}
\makeatother


\newcounter{numOfmails}


\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\newcommand{\nameMAIL}[1]{\stepcounter{numOfmails}\tikzmark{MAIL\thenumOfmails}\xdef\temp{#1}\global\expandafter\let\csname Mail\thenumOfmails\endcsname\temp\hbox{\phantom{\temp}}}
\newcommand{\MAILon}[1]{\xdef\temp{#1}\global\expandafter\let\csname MAILon\thenumOfmails\endcsname\temp\hbox{\phantom{\temp}}\tikzmark{MAILon\thenumOfmails}}

\newcommand{\LinkAllMails}{\begin{tikzpicture}[overlay,remember picture]\foreach \i in {1,...,\thenumOfmails}{
\path  ($(MAIL\i.north west)+(0,{\myfsize/1.5 pt})$) rectangle node[anchor=center] (NODE\i){} ($(MAILon\i.south east)$);
\node at (NODE\i) {\href{mailto:\csname Mail\i\endcsname@\csname MAILon\i\endcsname}{\csname Mail\i\endcsname\phantom{@}\csname MAILon\i\endcsname}};}\end{tikzpicture}}

\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1\textwidth}}



\begin{document}
\centering

\noindent

\renewcommand{\arraystretch}{1.2}

\begin{tabular}{L{.03}|r@{}>{@}L{0.5}}
  \toprule
  \textbf{\#} &\multicolumn{2}{c}{\textbf{w/ phantom}}\\
  \midrule
  1     & \nameMAIL{wab}&\MAILon{gmail.com}\\
  2     & \nameMAIL{1ab}&\MAILon{gmail.com}\\
  3           &\nameMAIL{ab}&\MAILon{gmail.com}\\
  4           &\nameMAIL{abdul}&\MAILon{hotmail.com}\\
  5           &\nameMAIL{koleygr}&\MAILon{private.com}\\
  \bottomrule
\end{tabular}
\LinkAllMails


\setcounter{numOfmails}{0}
\begin{tabular}{L{.03}|r@{}>{@}L{0.5}}
  \toprule
  \textbf{\#} &\multicolumn{2}{c}{\textbf{w/ phantom}}\\
  \midrule
  6     & \nameMAIL{wa7b1}&\MAILon{gmail.com}\\
  7     & \nameMAIL{1ab}&\MAILon{gmail.com}\\
  8           &\nameMAIL{3ab}&\MAILon{gmail.com}\\
  9           &\nameMAIL{abdu5l}&\MAILon{hotmail.com}\\
  10           &\nameMAIL{koleygr}&\MAILon{nottooprivate.com}\\
  \bottomrule
\end{tabular}
\LinkAllMails


\end{document}

Result:

enter image description here

Old answer:

According to @egreg's comment the \mbox command already "gives" a \leavemode and so there is no reason for the \leavemode in front of the \mbox command. (I just leaving it as is for now).

Check this:

\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           &\leavevmode\mbox{\ttfamily\phantom{xxx}\mail{a@tex.c}}\\
  2           &\leavevmode\mbox{\ttfamily\phantom{xx}\mail{ab@tex.co}}\\
  3           &\leavevmode\mbox{\ttfamily\mail{abcd@tex.com}}\\
  \bottomrule
\end{tabular}

\begin{tabular}{L{.03}|L{.5}}
  \toprule
  \textbf{\#} & \textbf{w/ phantom}\\
  \midrule
  1           &\leavevmode\mbox{\ttfamily{\color{red}xxx}\mail{a@tex.c}}\\
  2           &\leavevmode\mbox{\ttfamily{\color{red}xx}\mail{ab@tex.co}}\\
  3           &\leavevmode\mbox{\ttfamily\mail{abcd@tex.com}}\\
  \bottomrule
\end{tabular}
\end{document}

You had the next problems:

  1. According to the selected answer of the question \phantom inside tabularx you have to add a \leavemode command to use phantom inside your tabular.
  2. Your command for the \mail had some tokens that created extra space.
  3. You can use an \mbox to avoid extra space of the heperref function for the mail.

    Output:

enter image description here

  • 1
    \mbox does \leavevmode, so it's not necessary to add it. – egreg Sep 27 '17 at 10:54
  • Thanks @egreg... I did it step by step for his mistakes and did not realized that. I will edit. – koleygr Sep 27 '17 at 11:01
  • I marked your answer as correct because all that was required to fix my issues was to remove the spaces added by the mail command definition (add % to the line breaks...). Your code might be overkill for this purpose but it is a nice hack. That being said, you could have stuck with ttfamily to avoid letter spacing issues, and it requires to split the address in two columns which causes issues if someone were to try and copy and paste the data from the pdf. – Florian Oct 2 '17 at 11:44
1

For anyone looking for a simple fix to the issue described, there is only two steps to take:

  1. Add % to the line breaks in the mail command definition to avoid extra space;
  2. Use leavevmode to stack blocks horizontally and prevent line breaks in the table.

Demo

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 \& leavevmode} \\
  \midrule
  1           & {\leavevmode\ttfamily\phantom{xxx}\mail{a@tex.c}}   \\
  2           & {\leavevmode\ttfamily\phantom{xx}\mail{ab@tex.co}}  \\
  3           & {\leavevmode\ttfamily\mail{abcd@tex.com}}           \\
  \bottomrule
\end{tabular}

\end{document}

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.