1

I use this code:

\begin{tabularx}{\textwidth}{c|X} 
  \emph{17:49} & Very long text...
\end{tabularx}

to get this (I want to use LaTeX to keep a personal diary and notes should look like that): This is what I want to get using custom environment

I tried to define a custom environment (#1 is the time when note was written):

\newenvironment{note}[1]
{
\begin{tabularx}{\textwidth}{c|X} 
  \emph{#1} & 
}
{
\end{tabularx} 
}

But it doesn't work. Here's what log file says me:

Runaway argument?

! File ended while scanning use of \TX@get@body.
<inserted text> 
                \par 
<*> test.tex
schtandard
  • 14,892

2 Answers2

3

The tabularx documentation states

This mechanism of grabbing an environment body does have the disadvantage (shared with the AMS alignment environments) that you can not make extension environments by code such as

\newenvironment{foo}{\begin{tabularx}{XX}}{\end{tabularx}}

as the code is looking for a literal string \end{tabularx} to stop scanning. Since version 2.02, one may avoid this problem by using \tabularx and \endtabularx directly in the definition:

\newenvironment{foo}{\tabularx{XX}}{\endtabularx}

The scanner now looks for the end of the current environment (foo in this example.) There are some restrictions on this usage, the principal one being that \endtabularx must not be inside any { } pairs ao that the code before \endtabularx may be extracted and added to the table body (prior to version 2.09 \endtabularx had to be the first token of the ‘end code’ of the environment).

Thus, you can define your environment as

\newenvironment{note}[1]{%
    \par\noindent
    \tabularx{\textwidth}{c|X}%
    \emph{#1}   &%
}{%
    \endtabularx%
}

I added \par\noindent to the definition. \par ends the previous paragraph and \noindent makes sure the entry is not indented by \parindent (this would lead to an overfull \hbox).

schtandard
  • 14,892
0

Use \NewEnviron instead of \newenvironment.

\NewEnviron{note}[1]{
  \begin{tabularx}{\textwidth}{c|X} 
    \emph{#1} & \BODY
  \end{tabularx} 
}

A full example:

\documentclass{article}
\usepackage{tabularx}
\usepackage{environ}

\NewEnviron{note}[1]{ \begin{tabularx}{\textwidth}{c|X} \emph{#1} & \BODY \end{tabularx} }

\begin{document} \begin{note}{text} more text \end{note} \end{document}

muzimuzhi Z
  • 26,474
  • 1
    Hi and welcome. I notice that \NewEnviron is not one of the standard commands. Maybe you should edit your answer and, say, explain where this command came from. Perhaps make minimal working example (a complete code that can be compiled). – Symbol 1 Jun 21 '20 at 23:15
  • Sadly I'm new to Latex, and didn't even know that there were standard comands. I just had this problem and found an alternative solution quite hidden. Feel free to edit it explaining it better. – Maurici Abad Jun 22 '20 at 03:14