In Working with margins egreg proposed the below environment for gathering the maximal width of a description
-label. Unfortunately this code only works as long as the environment xdesc
is not nested. In case of nesting (see below) the code will return
LaTeX Error: Something's wrong--perhaps a missing \item.
Here is a nestified version of the code:
\documentclass[12pt]{article}
\usepackage[a4paper, margin=2cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[shortlabels]{enumitem}
\usepackage[spanish]{babel}
\newlength{\xdescwd}
\usepackage{environ}
\makeatletter
\NewEnviron{xdesc}{%
\vbox{\hbadness=\@M \global\xdescwd=0pt
\def\item[##1]{%
\settowidth\@tempdima{\textbf{##1}:}%
\ifdim\@tempdima>\xdescwd \global\xdescwd=\@tempdima\fi}
\BODY}
\begin{description}[leftmargin=\dimexpr\xdescwd+.5em\relax,
labelindent=0pt,labelsep=.5em,
labelwidth=\xdescwd,align=left]\BODY\end{description}}
\makeatother
\begin{document}
En un contexto determinado, las palabras poseen, además de su
significado literal, también una dimensión intencional. Un texto es la
manifestación lingüística de la intención del emisor dirigida a un
receptor. Si consideramos, pues, los actos ilocutivos implicados, un
texto cumplirá funciones diferentes.
La función textual es el efecto de los textos en una situación
comunicativa según los objetivos que queremos alcanzar. Un texto puede
desempeñar las siguientes funciones:
\begin{xdesc}
\item[Representativa:] llamada también asertiva, cuando se afirma o se
\begin{xdesc}
\item[Prescriptiva:] cuando se establecen normas. anuncia algo que se
considera verdadero.
\end{xdesc}
\item[Informativa:] cuando se presenta algo de manera neutra, sin
énfasis que pueda denotar emotividad, como por ejemplo un titular
de una noticia.
\item[Prescriptiva:] cuando se establecen normas. En la legislación, por
ejemplo, se emplean formas modales deónticas (deber + infinitivo)
\item[Persuasiva:] cuando se trata de convencer al interlocutor
\item[Comisiva:] cuando se promete, jura, ofrece, etc.
\item[Fática:] cuando el emisor se centra en el interlocutor y procura
mantener el contacto con él, como por ejemplo “Oye…”
\item[Expresiva:] cuando el emisor se alegra, agradece, felicita.
\end{xdesc}
\end{document}
Beyond that even the original code produces a strange duplication of the text of the items above the xdesc
-environment.
Since I really would like to implement the idea of egreg's code in an environment of my own any kind of help would be appreciated.
[Update 2016/12/04] Here is an addition to my original question concerning the answers of egreg and Andrew below:
Unfortunately after further testing in the wild the following problem came up: egreg's xdesc
breaks compatibility with the normal enumerate
-environment. Within the normal environment it is possible to have \items without any text. But in xdesc
a textless item disappears. (The same is true of Andrew's code.) I tried some modifications without success. So I have no clue how to prevent xdesc
from eating up textless items. Since I need the functionality of enumerate, any help would be welcome. Here is a MWE:
\documentclass[12pt]{article}
\usepackage[a4paper, margin=2cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[spanish]{babel}
\usepackage{enumitem}
\newlength{\xdescwd}
\usepackage{environ}
\makeatletter%
\NewEnviron{xdesc}{% I only changed "description" to "enumerate"
% and did some formatting
\setbox0=\vbox{%
\hbadness=\@M \global\xdescwd=0pt
\RenewEnviron{xdesc}{}%
\def\item[##1]{%
\settowidth\@tempdima{##1}%
\ifdim\@tempdima>\xdescwd \global\xdescwd=\@tempdima\fi}
\BODY%
}%
\begin{enumerate}[leftmargin=\dimexpr\xdescwd+.5em\relax,
labelindent=0pt,labelsep=.5em,
labelwidth=\xdescwd,align=left]
\BODY%
\end{enumerate}%
}
\makeatother
\begin{document}
\parindent0pt
Here is what \textbf{enumerate} does:
\begin{enumerate}
\item[(DF)] %llamada también asertiva, cuando se afirma o se
\begin{enumerate}
\item[(1)] cuando se establecen normas. anuncia algo que se
considera verdadero.
\item[(1a)] cuando se establecen normas. anuncia algo que se
considera verdadero.
\end{enumerate}
\end{enumerate}
Here is what \textbf{xdesc} does:
\begin{xdesc}
\item[(DF)] %llamada también asertiva, cuando se afirma o se
\begin{xdesc}
\item[(1)] cuando se establecen normas. anuncia algo que se
considera verdadero.
\item[(1a)] cuando se establecen normas. anuncia algo que se
considera verdadero.
\end{xdesc}
\end{xdesc}
\end{document}
Here's the output:
[End of Update 2016/12/04]
enumerate
environment inxdesc
.enumerate
inxdesc
, butxdesc
inxdesc
. The enumerate-example is only meant as a contrast case to make the difference obvious. (Thanks for the hint to open up a new question in case of need for further clarification.)\BODY
in ahbox
prevents the code from eating up items without text. Hopefully this doesn't have unwanted side-effects...