# Hide tables and figures and keep text and vice versa?

Is there an easy way to hide all text and only keep tables and figures and vice versa? My figures and tables are referenced with \ref and embedded with \input.

• So, basically, all input lines except those that start with \input should be ignored? Do you input anything with \input (sorry, couldn't resist the pun...) other than figure and table environments? – Mico Dec 14 '17 at 19:56
• Do you want to preserve the space ocuppied by text, using your tables and figures' version? – Przemysław Scherwentke Dec 14 '17 at 20:01
• @Mico Yes, I also add chapters (from main) and sections (from chapters) with input. You made me realise how I can do it. I can grep anything in the sections, which is not a line of text and compile those modified tex files. Not a super easy solution, but doable. – tommy.carstensen Dec 14 '17 at 20:12
• @Przemyslaw Either way works fine. I guess I could somehow use a white font? Preferably I want to remove the space if possible. – tommy.carstensen Dec 14 '17 at 20:14
• @tommy.carstensen - please see the answer I just posted. – Mico Dec 14 '17 at 22:35

Assuming you can use LuaLaTeX, the following solution may be of interest to you. It sets up two user macros, called \HideAllFigsAndTabs and \ShowOnlyFigsAndTabs. It goes (hopefully) without saying that at most one of the two LaTeX macros should be executed in a document.

The LaTeX macros' sole job is to assign one or the other of the Lua functions hide_all_figs_and_tabs and show_only_figs_and_tabs to the process_input_buffer callback. Once assigned to this callback, the active Lua function acts like a pre-processor on the input stream, before (La)TeX normal processing even begins.

The only restriction on input is that only one of four instructions \begin{figure}, \end{figure}, \begin{table}, and \end{table} is allowed to occur in any given line. Hopefully, that's not a binding restriction on your coding habits.

% !TEX TS-program = lualatex
\RequirePackage{filecontents}
%% create two "dummy" tex files
\begin{filecontents}{myfig.tex}
\begin{figure}[h]
\caption{Hello World}
\end{figure}
\end{filecontents}

\begin{filecontents}{mytab.tex}
\begin{table}[h]
\caption{Goodbye World}
\end{table}
\end{filecontents}

\documentclass{article}

%% Lua-side code
\usepackage{luacode}
\begin{luacode*}

in_fig_or_tab = false

function hide_all_figs_and_tabs ( s )
if   s:find ( "\\begin{figure" )
or s:find ( "\\begin{table"  ) then
in_fig_or_tab = true
return ""
elseif s:find ( "\\end{figure" )
or s:find ( "\\end{table"  ) then
in_fig_or_tab = false
return ""
elseif in_fig_or_tab == true then
return ""
end
return s
end

function show_only_figs_and_tabs ( s )
if   s:find ( "\\begin{figure" )
or s:find ( "\\begin{table"  ) then
in_fig_or_tab = true
return s
elseif s:find ( "\\end{figure" )
or s:find ( "\\end{table"  ) then
in_fig_or_tab = false
return s
-- Must also enable all lines that contain '\input'
-- statements or an '\end{document}' directive:
elseif s:find ( "\\input" )
or s:find ( "\\end{document}" )
or in_fig_or_tab == true then
return s
end
-- If none of the above is true, return empty string:
return ""
end

\end{luacode*}
%% LaTeX-side code
\newcommand\HideAllFigsAndTabs{\directlua{
hide_all_figs_and_tabs, "hide_all_figs_and_tabs" )}}
\newcommand\ShowOnlyFigsAndTabs{\directlua{
show_only_figs_and_tabs, "show_only_figs_and_tabs" )}}

\usepackage{lipsum} % for filler text

\begin{document}

%% Execute *only one* of the following two instructions:
%\HideAllFigsAndTabs
\ShowOnlyFigsAndTabs

\lipsum[1]

\input myfig

\lipsum[2]

\input mytab

\lipsum[3]

\end{document}
`