GeneralIs this possible? Through a new environment or other means?

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
sol_kanar
Posts: 4
Joined: Tue Jun 22, 2010 4:37 pm

Is this possible? Through a new environment or other means?

Post by sol_kanar »

Hi!

I am relatively new to LaTeX, and I have a very "strange" question. I tried a forum search, but I did not found an answer.

My question is: is there a way or a package (or something similar) to create this effect?

Code:

Code: Select all

\begin{wannabe-environment}
\Name Alfred
\Surname Hitchcock
\Age 89
\end{wannabe-environment}
Output:

Code: Select all

Name: Alfred  Surname: Hitchcock
Age: 89
The same output should be provided even if the "internal parameters" are given in a different order, e.g. code like this:

Code: Select all

\begin{wannabe-environment}
\Age 89
\Surname Hitchcock
\Name Alfred
\end{wannabe-environment}
shuld provide the same output as above. This should be only a "feature", able to be inserted in a single point of a document.

Can you help me? Thank you anyway for your time :-)

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

kaiserkarl13
Posts: 707
Joined: Tue Mar 25, 2008 5:02 pm

Is this possible? Through a new environment or other means?

Post by kaiserkarl13 »

Sure: I enjoy a challenge! I'm borrowing some inspiration from the \title, \author, etc. commands as well as \maketitle:

Code: Select all

\documentclass{article}

\makeatletter
\newenvironment{wannabe-environment}{
\def\Name##1{\gdef\@Name{##1}}
\def\@Name{\@latex@error{No \noexpand\Name given}\@ehc}
\def\Surname##1{\gdef\@Surname{##1}}
\def\@Surname{\@latex@error{No \noexpand\Surname given}\@ehc}
\def\Age##1{\gdef\@Age{##1}}
\def\@Age{\@latex@error{\noindent No \noexpand\Age given}\@ehc}
\long\def\makenames{\noindent Name:  \@Name\qquad Surname:  \@Surname\\
Age:  \@Age}
}
{\makenames}
\makeatother

\begin{document}
  \begin{wannabe-environment}
    \Name{Alfred}
    \Surname{Hitchcock}
    \Age{89}
  \end{wannabe-environment}

  \begin{wannabe-environment}
    \Name{Alfredo}
    \Surname{Hitchcockadoodledoo}
    \Age{898}
  \end{wannabe-environment}
\end{document}
sol_kanar
Posts: 4
Joined: Tue Jun 22, 2010 4:37 pm

Re: Is this possible? Through a new environment or other mea

Post by sol_kanar »

That was insightful! As I guessed, it needed a greater knowledge of LaTeX than mine, but now that I have an example I can start to work from here.

Thanks a lot! :-)
sol_kanar
Posts: 4
Joined: Tue Jun 22, 2010 4:37 pm

Is this possible? Through a new environment or other means?

Post by sol_kanar »

And what if I wanted to introduce optional arguments? So to speak, arguments that could or could not be included in my {wannabe-environment} but in any case should not raise errors? An example:

Code: Select all

\begin{wannabe-environment}
\Name{Alfred}
\Surname{Hitchcock}
\Age{89}
\Nationality{English}
\end{wannabe-environment}
Output:

Code: Select all

Name: Alfred  Surname: Hitchcock
Nationality: English  Age: 89
While if \Nationality is missing:

Code: Select all

\begin{wannabe-environment}
\Name{Alfred}
\Surname{Hitchcock}
\Age{89}
\end{wannabe-environment}
the output should be:

Code: Select all

Name: Alfred  Surname: Hitchcock
Age: 89

Another interesting feature would be the repetition of arguments, for example:

Code: Select all

\begin{wannabe-environment}
\Name{Alfred}
\Surname{Hitchcock}
\Age{89}
\Friend{James}
\Friend{Luke}
\Friend{Hamal}
\end{wannabe-environment}
with output:

Code: Select all

Name: Alfred  Surname: Hitchcock
Age: 89
Friends:
- James
- Luke
- Hamal
Can these also be obtained?

Thanks in advance to whoever will reply :-)
sol_kanar
Posts: 4
Joined: Tue Jun 22, 2010 4:37 pm

Is this possible? Through a new environment or other means?

Post by sol_kanar »

Update: after some research, I found a solution for the "optional arguments".

Code: Select all

\makeatletter
\newenvironment{wannabe-environment}{
\def\Name##1{\gdef\@Name{##1}}
\def\@Name{\@latex@error{No \noexpand\Name given}\@ehc}
\def\Surname##1{\gdef\@Surname{##1}}
\def\@Surname{\@latex@error{No \noexpand\Surname given}\@ehc}
% -- optional parameter
\def\Age##1{\gdef\@Age{##1}}
\global\let\@Immunita\@empty
% --
\long\def\makenames{\noindent Name:  \@Name\qquad Surname:  \@Surname\\
\ifx\@Age\@empty
  \relax
\else
  Age:  \@Age
\fi%}
}
{\makenames}
\makeatother
I will keep searching for the implementation of repeated parameters. Thank you in advance for any hint you could provide! :-)
Post Reply