Are there "methods/functions" in Latex?
LaTeX has macros, which are different than functions in important ways but can serve the purpose you want. One important thing to note is that the document is evaluated top to bottom and definitions are made as Latex sees them, so you must define your macros before you call them. For instance:
\documentclass{article}
\newcommand\questionone{stuff}
\newcommand\questiontwo{second stuff}
\newcommand\questionthree{third stuff}
\begin{document}
\questionone
\questiontwo
\questionthree
\end{document}
The more common thing that people do that allows for modularity is to put the questions in separate files, say question1.tex
, question2.tex
, and question3.tex
, then \input
or \include
the files. There is a command \includeonly
which restricts which includes are actually done so the following will only input question2.tex
:
\documentclass{article}
\includeonly{question2}
\begin{document}
\include{question1}
\include{question2}
\include{question3}
\end{document}
Please see EDIT below for what I'd actually recommend ...
I don't recommend doing it this way, but you could, if you wanted to define everything and then just say \main
in your document itself.
\documentclass{article}
\newcommand*\main{%
\begin{enumerate}
\item\questionone
\item\questiontwo
\item\questionthree
\end{enumerate}%
}
\newcommand*\questionone{%
Here is my response to the first question:
The best response ever!%
}
\newcommand*\questiontwo{%
Here is my response to the second question:
The second best response ever!%
}
\newcommand*\questionthree{%
Here is my response to the third question:
The third best response ever!%
}
\begin{document}
\main
\end{document}
As others have said, TeX uses macros
. LaTeX 3 introduces the concept of functions and variables, but they are all still really just macros. LaTeX generally (the current version is 2e) has commands and environments but, again, they are all macros.
EDIT
I would actually recommend using a different approach. As suggested in Hood Chatham's answer, I'd put the responses into separate files: question1.tex
, question2.tex
, question3.tex
, questionABX.tex
and so on. Then I would define a macro to take a list of questions to be included
\main{<comma-separated list of questions>}
For example,
\main{3,ABX}
or
\main{2,1,3}
or whatever.
Here's an example using LaTeX 3's syntax:
\begin{filecontents}{question1.tex}
Here is my response to the first question:
The best response ever!
\end{filecontents}
\begin{filecontents}{question2.tex}
Here is my response to the second question:
The next best response ever!
\end{filecontents}
\begin{filecontents}{question3.tex}
Here is my response to the third question:
The last best response ever!
\end{filecontents}
\begin{filecontents}{questionABX.tex}
Here is my response to another question:
This is another great response!
\end{filecontents}
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\clist_new:N \l_phillip_qlist_clist
\msg_new:nnn { phillip ~ questions } { question file not found } {
phillip ~ questions ~::~question~#1~requested~but~question#1.tex~not~found.~\msg_line_context:.
}
\NewDocumentCommand \main { m }
{
\phillip_print_responses:n { #1 }
}
\cs_new_protected_nopar:Nn \phillip_print_responses:n
{
\clist_set:Nn \l_phillip_qlist_clist { #1 }
\clist_map_inline:Nn \l_phillip_qlist_clist
{
\file_if_exist:nTF { question##1 }
{
\file_input:n { question##1 }
\par
}
{
\msg_fatal:nnn { phillip ~ questions } { question file not found } { ##1 }
}
}
}
\ExplSyntaxOff
\begin{document}
\main{1,2,,3}
\hrule
\main{1,2}
\hrule
\main{3,1}
\hrule
\main{2,ABX}
\end{document}