2

Is it possible to create an environment usable in this way...?

\begin{myEnvironment}

\{title}[This is a title]
\{year}[2016]
\{content}[Lorem ipsum dolor sit amet, consectetur adipisci elit,
sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut
enim ad minim veniam, quis nostrum exercitationem ullam corporis
suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur.]

\end{myEnvironment}

...or this way...?

\begin{myEnvironment}

\{title} This is a title
\{year} 2016
\{content} Lorem ipsum dolor sit amet, consectetur adipisci elit,
sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut
enim ad minim veniam, quis nostrum exercitationem ullam corporis
suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur.

\end{myEnvironment}

...or something similar?

And if I want I can omit a field (for example title).

The output should be something like this (for example):

  • Title: This is a title
  • Year: 2016

Lorem ipsum dolor sit amet, consectetur adipisci elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur.

  • Is there a problem with the more standard approach: \documentclass{article} \usepackage{lipsum} \begin{document} \begin{itemize} \item \textbf{Title:} This is a title \item \textbf{Year:} 2016 \end{itemize} \lipsum[1] \end{document} – Steven B. Segletes Feb 23 '16 at 11:19
5

Yes, it's possible and also with a friendly user level syntax.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentEnvironment{myenv}{m} % <--- choose a better name
 {
  % process the keys
  \keys_set:nn { myenv } { #1 }
  % print them
  \par\addvspace{\topsep}
  \noindent\makebox[4em][l]{\textbullet\ \textbf{Title:}}~\l_myenv_title_tl
  \\*
  \noindent\makebox[4em][l]{\textbullet\ \textbf{Year:}}~\l_myenv_year_tl
  \\*
  \ignorespaces
 }
 {
  \par\addvspace{\topsep}
 }

% define the keys
\keys_define:nn { myenv }
 {
  title .tl_set:N = \l_myenv_title_tl,
  year  .tl_set:N = \l_myenv_year_tl,
  title .value_required:n = true,
  year  .value_required:n = true,
 }
\ExplSyntaxOff

\begin{document}

\begin{myenv}{
  title = This is a title,
  year  = 2016,
}
Lorem ipsum dolor sit amet, consectetur adipisci elit, 
sed eiusmod tempor incidunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrum exercitationem
ullam corporis suscipit laboriosam, nisi ut aliquid ex ea
commodi consequatur.
\end{myenv}

\end{document}

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.