<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def set_variable(self, name, value):
 r"""Add a variable which can be used inside the document. Variables are defined before the preamble. If a variable with that name has already been set, the new value will override it for future uses. This is done by appending ``\renewcommand`` to the document. Args ---- name: str The name to set for the variable value: str The value to set for the variable """


        name_arg = "\\" + name
        variable_exists = False

        for variable in self.variables:
            if name_arg == variable.arguments._positional_args[0]:
                variable_exists = True
                break

        if variable_exists:
            renew = Command(command="renewcommand",
                            arguments=[NoEscape(name_arg), value])
            self.append(renew)
        else:
            new = Command(command="newcommand",
                          arguments=[NoEscape(name_arg), value])
            self.variables.append(new)