5

I would like to create a macro that converts its paramater into an image with the goal that the text within this macro is not found using the search feature built into standard PDF viewers.

Thus, searching the resulting document for "text" should produce only two matches and not four. That is, any text in the highlighted box should ignored.

enter image description here

Use-Case

I generate a large index of files and want to be able to search by the index entry only (and not the files names).

MWE:

\documentclass{article}
\usepackage{tikz}

\tikzset{Node Style/.style={draw=red, thin, fill=yellow!50, anchor=base, inner sep=1pt,}}
\newcommand{\NonSearchableText}[2][]{%
    \tikz[baseline] \node [Node Style, #1] at (0,0) {#2};%
}%

\begin{document}
    \textbf{searchable text}
    \indent\NonSearchableText{non-searchable-text}

    \textbf{searchable text}
    \indent\NonSearchableText{non-searchable-text}
\end{document}

Desired Ouput

The desired output can be created by using the first file here to create the image non-searchable-text.png which is included by the second.

  1. File name: non-searchable-text.tex

    %%% https://tex.stackexchange.com/questions/34054/tex-to-image-over-command-line
    \documentclass[convert={density=300,outext=.png}]{standalone}
    \usepackage{tikz}
    
    \tikzset{Node Style/.style={draw=red, thin, fill=yellow!50, anchor=base, inner sep=1pt,}}
    \newcommand{\NonSearchableText}[2][]{%
        \tikz[baseline] \node [Node Style, #1] at (0,0) {#2};%
    }%
    
    \begin{document}
        \NonSearchableText{non-searchable-text}
    \end{document}
    
  2. Main File:

    \documentclass{article}
    \usepackage{graphicx}
    \usepackage[export]{adjustbox}
    
    %% https://tex.stackexchange.com/questions/98297/create-command-to-inline-an-image-in-a-question
    \newcommand*{\Image}[1]{\includegraphics[valign=m]{#1}}%
    
    \begin{document}
        \textbf{searchable text}
        \indent\Image{non-searchable-text.png}
    
        \textbf{searchable text}
        \indent\Image{non-searchable-text.png}
    \end{document}
    
Peter Grill
  • 223,288
  • The problem is that a PDF based image (like TikZ) probably includes the text as text In the (vector graphics) image. You would need to produce a rasterized image, which means using a graphics editor to convert. – John Kormylo Nov 05 '18 at 14:01
  • You could precompute rasterized images of each letter. There might be something useful in https://tex.stackexchange.com/questions/29402/how-do-i-make-my-document-look-like-it-was-written-by-a-cthulhu-worshipping-madm?s=2|33.7384 – John Kormylo Nov 05 '18 at 14:10

1 Answers1

1

The accsupp package can do this. The syntax is \BeginAccSupp{method=escape,ActualText=<searchable non-visualized text>}<non-searchable visualized text>\EndAccSupp{}.

\documentclass{article}
\usepackage{tikz,accsupp}

\tikzset{Node Style/.style={draw=red, thin, fill=yellow!50, anchor=base, inner sep=1pt,}} \newcommand{\NonSearchableText}[2][]{% \tikz[baseline] \node [Node Style, #1] at (0,0) {% \BeginAccSupp{method=escape,ActualText=}#2.\EndAccSupp{}};% }%

\begin{document} \textbf{searchable text} \indent\NonSearchableText{non-searchable-text}

\textbf{searchable text}
\indent\NonSearchableText{non-searchable-text}

\end{document}

The visual output is, as before

enter image description here

The searchable text of the document includes the following:

searchable text
searchable text
  • 1
    Wow!. Thats a lot simpler that the hack I had thought of. Thanks. – Peter Grill Dec 26 '21 at 19:31
  • 1
    You are welcome, @PeterGrill. It is a useful package, allowing various clever applications, such as https://tex.stackexchange.com/questions/233390/in-which-way-have-fake-spaces-made-it-to-actual-use/233397#233397 or https://tex.stackexchange.com/questions/198516/is-there-such-thing-as-visual-only-whitespace/198519#198519 – Steven B. Segletes Dec 26 '21 at 19:34