Skip to content

Instantly share code, notes, and snippets.

@tnaia
Last active August 29, 2015 14:02
Show Gist options
  • Save tnaia/4931bf4d7d2ecdbd5a45 to your computer and use it in GitHub Desktop.
Save tnaia/4931bf4d7d2ecdbd5a45 to your computer and use it in GitHub Desktop.
Deferred printing. Montando uma tabela aos poucos, durante o documento.
\documentclass{article}
% Input and output encoding
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Contador
\newcounter{indice} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Open auxiliar output file for holding table lines
\newwrite\ans
\immediate\openout\ans=\jobname-temp
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% É preciso tomar cuidado com o unexpanded. Os parâmetros de
% AppendRequisito _precisam_ ser expandidos neste ponto do texto
% (afinal eles são redefinidos várias vezes).
\newcommand{\AppendRequisito}[2]{%
\immediate\write\ans{#1 & #2 \unexpanded{\\}}%
}%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\PrintRequisitos}{%
\begin{tabular}{cl}%
\textbf{Identificação} & \textbf{Requisito} \\%
\hline%
\immediate\closeout\ans\input{\jobname-temp}%
\hline%
\end{tabular}%
}%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Macros to be redefined inside of the Requisito environment and which
% are used by CriaRequisito.
\newcommand{\CmdReqNome}{}%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\CmdReqDescr}{}%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newenvironment{Requisito}{}{%
\CriaRequisito%
}%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\CriaRequisito}{%
% Write some text
O requisito \CmdReqNome\ (\CmdReqDescr) foi criado aqui.%
% And append info to auxiliar file
\AppendRequisito{\CmdReqNome}{\CmdReqDescr}%
}%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
Declaramos os requisitos\ldots
\begin{Requisito}
\renewcommand{\CmdReqNome}{Comida}
\renewcommand{\CmdReqDescr}{Para matar a fome}
\end{Requisito}
\begin{Requisito}
\renewcommand{\CmdReqNome}{Bebida}
\renewcommand{\CmdReqDescr}{Para a sede}
\end{Requisito}
\section{E aqui imprimimos os requisitos}
\PrintRequisitos
\end{document}
%% Autor: Tássio Naia dos Santos
%% Impressão defasada, ou retardada (Deferred printing),
%% baseado na resposta de egreg no [stackexchange][]
%%
%% [stackexchange]: http://tex.stackexchange.com/questions/23253/how-to-append-data-to-a-temporary-file
%%
%% Neste exemplo, a chamada de um comando escreve um certo
%% texto e acrescenta uma em um arquivo auxiliar. As linhas
%% que são escritas formam o corpo de uma tabela de voluntários.
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
%% New job (that is, file)
\newwrite\ans
\immediate\openout\ans=\jobname-temp
%% Esta macro expande para algum texto e uma invocação
%% do comando `AppendVolunteer`, que escreve no arquivo.
\newcommand{\AddVolunteer}[2]{%
\par\indent%
Acrescentando voluntário (#1)\ldots%
\AppendVolunteer{#1}{#2}}
%% Usamos unexpanded... mas eu não sei porque. No meu
%% computador não funciona se removo o unexpanded. Acho
%% que deve ter algo haver com o `\\` dentro da expressão.
%% (Aliás, isso é muito provável, já que o significado
%% de `\\` é diferente no texto e em um ambiente tabular;
%% sua expansão tem que ser postergada para o momento em
%% que a tabela está sendo impressa (`PrintVolunteers`).
\newcommand{\AppendVolunteer}[2]{%
\immediate\write\ans{\unexpanded{#1 & #2\\}}}
\newcommand{\PrintVolunteers}{%
\begin{tabular}{cc}%
\textbf{nome} & \textbf{atividade} \\\hline%
\immediate\closeout\ans\input{\jobname-temp}%
\hline\end{tabular}}
\begin{document}
\section{Voluntários}
Vamos imaginar que eu esteja fazendo uma lista de pessoas.
\AddVolunteer{José}{programação}
\AddVolunteer{Jandira}{programação}
\section{Tabela de voluntários}
\PrintVolunteers
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment