Defina 2 variáveis ​​e imprima-as

Defina 2 variáveis ​​e imprima-as

Estou apenas começando com o LaTeX e já estou lutando com as coisas básicas.

Eu crio 2 comandos, cada um atribuindo um valor a uma variável:

\newcommand{\firstname}[1]{\def\@firstname{#1}}
\newcommand{\lastname}[1]{\def\@lastname{#1}}

Essas variáveis ​​são definidas usando os comandos em algum lugar no início do documento:

\firstname{Kevin}
\lastname{De Coninck}

Quando tento imprimir o lastname, tudo funciona bem, quando tento imprimir o firstname, é retornado o seguinte erroUse of \@ doesn't match its definition. \@f

Estou imprimindo dentro do meu documento usando o seguinte código:

\begin{document}
    \@firstname
\end{document}

O que estou perdendo aqui?

Atualização: documento inteiro

% ----------------------------------------------------------------------
% -- CONFIGURATION
% ----------------------------------------------------------------------
\documentclass[11pt, a4paper]{article}

% ----------------------------------------------------------------------
% -- PACKAGES
% ----------------------------------------------------------------------
\RequirePackage{geometry}

% ----------------------------------------------------------------------
% -- COMMAND DEFINITIONS
% ----------------------------------------------------------------------

% Summary: Configure the person's name.
% Usage:   \name{<firstname>}{<lastname>}
%          firstname{<firstname>}
%          lastname{<lastname>}
\newcommand{\firstname}[1]{\def\@firstname{#1}}
\newcommand{\lastname}[1]{\def\@lastname{#1}}

% ----------------------------------------------------------------------
% -- LAYOUT CONFIGURATION
% ----------------------------------------------------------------------
\geometry{left=2.0cm, top=5.5cm, right=2.0cm, bottom=2.0cm, footskip=.5cm}

% ----------------------------------------------------------------------
% -- PERSONAL DATA
% ----------------------------------------------------------------------
\firstname{Kevin}
\lastname{De Coninck}

% ----------------------------------------------------------------------
% -- ACTUAL DOCUMENT
% ----------------------------------------------------------------------
\begin{document}
    \@firstname
    \@lastname
\end{document}

Responder1

Eu adicionei o \makeatletter...\makeatotherpar usual em torno das definições e do uso.

Por favor, tenha em mente também a proposta de Daleif: \CMPLXfirstnameparece ser melhor e mais fácil.

Eu também mudei \RequirePackagepara \usepackage(embora o primeiro também seja permitido, mas destinado apenas a pacotes, não a documentos normais)

Agora, @é um caractere especial reservado como parte de nomes de comandos internos que não devem ser facilmente acessíveis para um usuário normal de documento. Fora de pacotes ou arquivos de classe @não é considerada uma letra permitida para nomes de comandos, então \makeatletteraltera isso (temporariamente), permitindo @fazer parte de tais nomes e \makeatotherreverte isso.

% ----------------------------------------------------------------------
% -- CONFIGURATION
% ----------------------------------------------------------------------
\documentclass[11pt, a4paper]{article}

% ----------------------------------------------------------------------
% -- PACKAGES
% ----------------------------------------------------------------------
\usepackage{geometry}

% ----------------------------------------------------------------------
% -- COMMAND DEFINITIONS
% ----------------------------------------------------------------------

% Summary: Configure the person's name.
% Usage:   \name{<firstname>}{<lastname>}
%          firstname{<firstname>}
%          lastname{<lastname>}
\makeatletter
\newcommand{\firstname}[1]{\def\@firstname{#1}}
\newcommand{\lastname}[1]{\def\@lastname{#1}}

 \newcommand{\printname}{%
  \@ifundefined{@firstname}{}{\@firstname} \@ifundefined{@lastname}{}{\@lastname}%
}

\makeatother

% ----------------------------------------------------------------------
% -- LAYOUT CONFIGURATION
% ----------------------------------------------------------------------
\geometry{left=2.0cm, top=5.5cm, right=2.0cm, bottom=2.0cm, footskip=.5cm}

% ----------------------------------------------------------------------
% -- PERSONAL DATA
% ----------------------------------------------------------------------
\firstname{Kevin}
\lastname{De Coninck}

% ----------------------------------------------------------------------
% -- ACTUAL DOCUMENT
% ----------------------------------------------------------------------
\begin{document}
\makeatletter
    \@firstname\ \@lastname
\makeatother

Or \printname%
\end{document}

insira a descrição da imagem aqui

Responder2

Caso você não queira usar \makeatletter.. \makeatother, você pode usar
\csname..\endcsname:

\csname @firstname\endcsname →  \@firstname.
\csname @lastname\endcsname →  \@lastname.

Eu escrevi um pequeno wrapper para \csname..\endcsname:

A macro \CreateCsFromNamefunciona da seguinte maneira:

\CreateCsFromName<preceding tokens with no curly braces>{ControlSequence}
→  <preceding tokens with no curly braces>\ControlSequence.

Se você omitir <preceding tokens with no curly braces>, ou seja, se deixar em <preceding tokens with no curly braces>branco, então \CreateCsFromNameapenas construirá o token da sequência de controle a partir do nome da sequência de controle:

\CreateCsFromName{macro}→ \macro

Em vez de deixar <preceding tokens with no curly braces>em branco, você pode fornecer quaisquer comandos para (re)definir:

\CreateCsFromName\long\def{macro}→ \long\def\macro

\CreateCsFromName\long\def{macro}#1#2{First argument: #1, second argument: #2}
→ \long\def\macro#1#2{First argument: #1, second argument: #2}

\CreateCsFromName\newcommand*{macro}→ \newcommand*\macro

\CreateCsFromName\newcommand*{macro}[2]{First argument: #1, second argument: #2}
→ \newcommand*\macro[2]{First argument: #1, second argument: #2}

Exemplo:

\documentclass{article}

\newcommand\Exchange[2]{#2#1}
\newcommand\CreateCsFromName{}
\long\def\CreateCsFromName#1#{\romannumeral0\InnerCreateCsFromName{#1}}
\newcommand\InnerCreateCsFromName[2]{%
  \expandafter\Exchange\expandafter{\csname#2\endcsname}{ #1}%
}

% raise an error in case \@firstname and \@lastname are already defined:
\CreateCsFromName\newcommand*{@firstname}{}
\CreateCsFromName\newcommand*{@lastname}{}

% commands for silently redefining \@firstname and \@lastname:
\newcommand{\firstname}{\CreateCsFromName\def{@firstname}}
\newcommand{\lastname}{\CreateCsFromName\def{@lastname}}

\firstname{Kevin}
\lastname{De Coninck}

\begin{document}

\CreateCsFromName{@firstname} \CreateCsFromName{@lastname}

\end{document}

informação relacionada