# Restricting \input to avoid unpleasant duplication

Avoid inputting a file multiple times describes how to prevent a file from being \input more than once. I wonder, though, if there is a way to limit this to only preventing a file from being input if and only if it has been \input within the last, say, five pages... but allowing it to be \input if there are at least five pages since the last time.

I have a data set that associates files to be \input (diagrams) with data objects. I don't want to see the same diagram input multiple times in close succession (seeing the same diagram more than once on the same page, or within a few pages, is unpleasant), but am happy to see the diagram included again if associated with a data element sometime later.

For example, an atlas of US cities ordered by name might display a small map of the state the city is in. It's dead easy to always include the map, it's slightly trickier but not hard to include a map once and only once. I'm wondering if it's possible to make it so the map gets included the first time, then any attempt to \input it again ignored unless there are at least (some number) of pages between the current page and the last time it was \input.

• Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – user31729 Jul 18 '14 at 22:04

\ifnum\numexpr\c@page>\numexpr\uniquenameforthisfile+5\relax
\xdef\uniquenameforthisfile{\the\c@page}
\input{thisfile}
\fi


together with initialisising

\def\uniquenameforthisfile{0}


at the start

should not input the file twice within 5 pages (it would be a bit more accurate to use \pageref to get the page number but trickier to set up especialy if the page number is nor formatted as an integer.)

• if I'm reading this correctly, it compares the current page number to "last time I set \uniquenameforthisfile+5. If it finds that it is so then it defines \uniquenameforthisfile as the current page and inputs the file. Using \pageref would depend on the current page breaks and whatnot, and could cause the image to drop in and out for each build (potentially degenerate case perhaps?). I'm satisfied with 'close enough'; if it means I get the images at n+-1 or so I'm happy. Thanks David! – Keith Davies Jul 19 '14 at 7:41

Based on David's excellent response, I've put a wrapper around (the wrapper around) \input:

\def\limitinput#1{
\IfFileExists{#1}{
\expandafter\ifx\csname limit@#1\endcsname\relax
\expandafter\xdef\csname limit@#1\endcsname{\the\c@page}
\input{#1}{}
\else
\ifnum\numexpr\c@page>\numexpr\expandafter\csname limit@#1\endcsname+3\relax
\input{#1}
\expandafter\xdef\csname limit@#1\endcsname{\the\c@page}
\fi
\fi
}{}
}


Approximately:

• If the named file exists (named file may or may not be created by an external process, so don't assume it exists):

• If I have not seen (tried to input) this file before, input it and capture the page number I did it on.

• If I have have seen this file before, but it was more than (about) 3 pages ago, input it and capture the page number I did it on.

• If I have seen this file before, and it was pretty recent (no more than about 3 pages ago), do not input it and do not capture the page number.

This won't be precise. With page content shifting a bit between builds and content floating (both of which indicate using \pageref is more robust) it will not be strictly accurate in the page calculations... but as long as it's within a page or two it should be close enough. I can come back and revise it later if I need to.

• I'm not sure, but you may want an explicit \relax after each \numexpr. – Sean Allred May 19 '15 at 1:06