28

Publishing pdf manuscripts on the web with emails embedded in the file results in spam.

More generally, I am looking for a way to automate the conversion of a string to an image.

It's quite easy to solve this old problem using \includegraphics an image (more here). The trouble is that you need a distinct file and typesetting run, and you end up with unnecessary image files that you cannot delete carelessly lest you also delete non-generated images.

Can you think of a way to do this on the fly? It's understood that people will have to type the email addresses if they need to message you, but that's the point.

More briefly/concretely...

With what can one replace #1

\documentclass{article}
\newcommand\showasimage[1]{#1}
\begin{document}
\showasimage{[email protected]}
\end{document}

such that #1 appears as an image?

Edit

It would be really nice if the pdf file remains vectorial.

6
  • I think it might be better to do something like joe <at> foo <dot> bar so that people using screen reading software can still email you. If it is an image, there is not much you can do if you can't see it. (Or does PDF have a way to insert this as alternate text in the same way html does? Then you could provide this for screen readers and the image for everyone else.) That said: ctan.org/pkg/randtext will do what you want.
    – cfr
    Feb 14, 2014 at 1:56
  • Actually, randtext doesn't work here. I get no errors, the output is fine but copy-and-paste is not scrambled at all.
    – cfr
    Feb 14, 2014 at 2:04
  • Be careful that for some viewers/printers the text of the image (in this case the email) can look/print very different from the the rest of the text, even if the resolution of the image is high.
    – alfC
    Feb 14, 2014 at 3:26
  • 2
    Related: tex.stackexchange.com/questions/150375/…. While it doesn't use an image approach, it makes it so the copy/paste produces a junk email address. Feb 14, 2014 at 3:44
  • 2
    For the partiucular objective of hiding the email text, there is other approach tex.stackexchange.com/questions/95123/…. (But the question is good in general).
    – alfC
    Feb 14, 2014 at 5:58

2 Answers 2

24

The following solution does not (anymore) use ImageMagick's convert command as this would always create raster images, which will stand out from the text. Instead, it uses Ghostscript to vectorise the letters so that they appear like normal text but are in fact an uncopyable image. Note that you also have to run pdflatex with the --shell-escape switch enabled, so that the commands from \write18 will indeed be sent to the shell.

The image will be cropped so that it is placed on the base line (with descenders taken into account), and the font will match the one from the context. The temporary files (using a counter to allow more than one image on the same page) will be deleted at the end.

\documentclass{article}
\usepackage{graphicx}
\makeatletter
\newcount\@emailcount
\newcommand\showasimage[1]{%
  \global\advance\@emailcount 1
  \edef\x{email-\the\@emailcount}%
  \immediate\write18{echo '\unexpanded{\\font\\1=}\fontname\font' > \x.tex}%
  \immediate\write18{echo '\unexpanded{\\1#1\\nopagenumbers\\bye}' >> \x.tex}%
  \immediate\write18{pdftex \x.tex}%
  %\immediate\write18{convert -units PixelsPerInch -density 300 -trim \x.pdf \x.png}% raster
  \immediate\write18{gs -dNOCACHE -sDEVICE=epswrite -dQUIET -o \x.eps \x.pdf}% vector
  \immediate\write18{epstopdf --hires \x.eps}%
  \settodepth{\@tempdima}{#1}%
  \raisebox{-\the\@tempdima}{\includegraphics{\x.pdf}}%
}
\AtEndDocument{%
  \loop
    \immediate\write18{rm email-\the\@emailcount.*}%
    \advance\@emailcount-1
    \ifnum\@emailcount>0\repeat
}
\makeatother
\begin{document}
Email \showasimage{[email protected]} not copyable

\textit{match font and depth: \showasimage{[email protected]}} 

\end{document}

which yields (with the first line selected):

emails as images

7
  • Slightly more elaborate... Is this the 'microtype' solution?
    – jon
    Feb 14, 2014 at 3:34
  • Nice - I am thinking that the use of convert can be avoided with this idea. The standalone package can make trimmed output. Feb 14, 2014 at 3:35
  • @Hans-PeterE.Kristiansen Indeed! I have a hunch that a combination of standalone and either bashful or Robert's write18 (what is that!?) would make it possible to use a vector image. The result would then be indistinguishable from normal text inclusion (and the pdf file will likely be much smaller).
    – Calaf
    Feb 14, 2014 at 3:40
  • 1
    @Hans-PeterE.Kristiansen -- standalone uses ImageMagick by default as its conversion program. (Though it can be modified to use something else like Ghostscript.)
    – jon
    Feb 14, 2014 at 3:42
  • 1
    @jon 'micrographics', rather ...
    – Robert
    Feb 14, 2014 at 4:03
9

This works for me, but I don't know about your OS, so I'm not sure if it will work for you. Note that the command convert is part of ImageMagick. Compile with -shell-escape enabled.

\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage{bashful,graphicx}

\begin{document}

\bash
echo "[email protected]" | convert label:@- email.png
\END
\includegraphics{email.png}

\end{document}

That won't work in a \newcommand, however. But this seems to work (still on bash):

\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage{graphicx}

\newcommand{\showasimage}[2]{%
\immediate\write18{echo "#1" | convert label:@- #2.png}%
\includegraphics{#2.png}%
}

\begin{document}

\showasimage{[email protected]}{email2}

\end{document}
5
  • What does label:@- mean? man convert tells me that all options start with a -. Feb 14, 2014 at 2:32
  • @Hans-PeterE.Kristiansen -- It's to get a label from standard input; see here, at the end of the section.
    – jon
    Feb 14, 2014 at 2:42
  • Ok - I my mistake. Just simpler to write: convert label:#1 #2.png Feb 14, 2014 at 2:54
  • @Hans-PeterE.Kristiansen -- Sure. Same number of chars either way :). In TeX, at least. I'm used to thinking of convert mainly in a non-TeX usage...
    – jon
    Feb 14, 2014 at 2:58
  • 1
    How nice to discover the bashful package! I'm sure it will come in handy quite often.
    – Calaf
    Feb 14, 2014 at 3:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .