4

I'm "trying to change the "0.5" as needed. It would be perfect if this command could have an default value of 0.5.

Unfortunately it seems to be very difficult to create a new environment with the "align"-environment.

\def\blockbegin#1\blockend{
\begin{varwidth}[t]{0.5\textwidth} 
\begin{align*} #1
\end{align*}
\end{varwidth}~\hfill}

Edit: I'm still not happy because there are some recurring things I'd like to automate. Here is my application. It's simply displaying math homework in two columns.

How can I move the right box up /align vertically at the top?

\documentclass[12pt,ngerman]{article}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{amsmath,varwidth}

\newenvironment{block}[1][0.5]
{\varwidth{#1\textwidth} \csname align*\endcsname}
{\endalign\endvarwidth \hfill}

\newcommand{\void}{\vspace{3ex}
\rule[0cm]{\textwidth}{0.4pt}\\}

\begin{document}

2.b) Berechnen Sie die Fläche, die der Graph von $f$ mit der x-Achse im I. Quadranten einschließt.\\ \\
This is how it's done:\\ % can I include the `\\` in the FIRST ’block’-command
\begin{block}
 A(u) &= \int_{0}^{u} (f(x)) \cdot dx\\
 &= \int_{0}^{u} (3xe^{-x}) \cdot dx\\
 &= \Big[-3(x+1)e^{-x} \Big]_{0}^{u}\\
 &= \Big(-3(u+1)e^{-u} \Big) - \Big(-3(0+1)e^{-0} \Big)\\
 &= \Big( \frac{-3(u+1)}{e^u}  \Big) - \Big(-3 \cdot 1 \Big)\\
 &= \frac{-3(u+1)}{e^u} + 3 
 \end{block}
 \begin{block}
 A =& \lim\limits_{u \to \infty} \Big(A(u)\Big)\\
 =& \lim\limits_{u \to \infty} \Big(\frac{-3(u+1)}{e^u} + 3 \Big)\\ \\
 =& 3
\end{block}
\void % Can I include this at the end of the last ’block’

Here it goes..

\end{document}

enter image description here

I thought about it and this is the construct is what I'm actually using:

\newcommand{\exsection}[3]
{
 #1:\\
 \begin{block}
  #2
 \end{block}
 \hfill
 \begin{block}
  #3
 \end{block}
 \void
}
here it goes..
  • 1
    Welcome to TeX.SX! Usually we do not use greetings or thanks in our questions: this may look unpolite, at first, but it is part of the style of the site. The main concern, here, is to provide concise an effective information. – GuM Mar 18 '16 at 18:28
3

I'm not sure what's the purpose for this, but here it is:

\documentclass{article}
\usepackage{amsmath,varwidth}

\newenvironment{block}[1][0.5]
 {\varwidth{#1\textwidth}\csname align*\endcsname}
 {\endalign\endvarwidth}

\begin{document}

\fbox{xxx
\begin{block}[.25]
a & b\\
c & d
\end{block}
}

\fbox{xxx
\begin{block}[.5]
a & b\\
c & d
\end{block}
}

\fbox{xxx
\begin{block}
a & b\\
c & d
\end{block}
}

\end{document}

enter image description here

  • Of course, only the OP knows why (s)he insisted on using an argument delimited by \blockend – GuM Mar 18 '16 at 19:30
  • @GustavoMezzetti “Unfortunately it seems to be very difficult to create a new environment with the align environment.” It isn't. ;-) – egreg Mar 18 '16 at 20:29
  • @egreg Yes! It seems difficult but your answer works just fine ;) – Joel Duscha Mar 18 '16 at 21:10
  • 1
    @JoelDuscha: the key is to avoid using constructs like \begin{FOO} and \end{FOO} buried inside your definition, but to use only the “non-environmental” forms \FOO and \endFOO. For an explanation of why this is necessary (and sufficient), see amsmath.pdf, section 16.3 (type texdoc amsmath.pdf at the command line). – GuM Mar 19 '16 at 0:25
3

Maybe something like this? EDITED to make the width argument optional, and default to 0.5.

\documentclass{article}
\usepackage{varwidth,amsmath}
\newcommand\blockbegin[1][0.5]{\def\VWid{#1}\blockbeginX}
\def\blockbeginX#1\blockend{
\begin{varwidth}[t]{\VWid\textwidth} 
\begin{align*} #1
\end{align*}
\end{varwidth}~\hfill}
\begin{document}

xxx

\blockbegin
a & b\\
c & d
\blockend

xxx

\blockbegin[0.25]
a & b\\
c & d
\blockend

\end{document}

enter image description here

  • Thank you very much. That helped at least for my uses. What I was trying to say with ’optional argument’ was that it would be optimal for \VWid to have a default value like it is now. – Joel Duscha Mar 18 '16 at 19:21
  • @JoelDuscha Please see my improved revision that provides the optional argument, with default. – Steven B. Segletes Mar 18 '16 at 19:36
1

Well, it’s no big deal to make it work with an optional argument too; for example, using xparse:

\documentclass{article}
\usepackage{varwidth,amsmath}
\usepackage{xparse}

\NewDocumentCommand \blockbegin { O{0.5} u{\blockend} }
  {
    \begin{varwidth}[t]{#1\textwidth} 
      \begin{align*}#2\end{align*}
    \end{varwidth}~\hfill
  }



\begin{document}

xxx

\blockbegin[.5]
a & b\\
c & d
\blockend

xxx

\blockbegin[.25]
a & b\\
c & d
\blockend

xxx

\blockbegin
a & b\\
c & d
\blockend

\end{document}

A hand-crafted, pure-LaTeX2e implementation is also possible, of course.

  • This looks like a more professional solution. But I'm afraid I do not understand everything of it. Where exactly is the xparse part used? For example I don't understand that whole \NewDocumentCommand line. – Joel Duscha Mar 18 '16 at 19:30
  • @JoelDuscha: That’s exactly the part that uses xparse. (;-) But, really, why don’t you use an environment, as egreg suggests, instead of a delimited argument? – GuM Mar 18 '16 at 19:34
1

Here is a typical TeX-like implementation that provides an optional argument for \blockbegin:

enter image description here

\documentclass{article}

\usepackage{varwidth,amsmath}

\makeatletter
\def\blockbegin{\@ifnextchar[\blockbegin@i{\blockbegin@i[0.5]}}
\def\blockbegin@i[#1]#2\blockend{%
  \begin{varwidth}[t]{#1\textwidth} 
    \begin{align*} #2
    \end{align*}
  \end{varwidth}%
}
\makeatother

\begin{document}

xxx

\blockbegin
  a & b\\
  c & d
\blockend

xxx

\blockbegin[.25]
  a & b\\
  c & d
\blockend

\end{document}
  • Exactly what I had in mind when I spoke of “pure LaTeX2e implementation”. :-) – GuM Mar 18 '16 at 19:26
  • So, is this preferable because it is a “pure LaTeX2e implementation” ? Or should I use xparse? – Joel Duscha Mar 18 '16 at 19:31
  • @JoelDuscha: The solution I would recommend is egreg’s, unless you have a specific reason to use \blockend as the argument delimiter (for instance, compatibility with legacy code); in this case, use Werner’s answer or mine, whichever you understand better. Edit: I wrote my answer only to honor your request for a delimited argument. – GuM Mar 18 '16 at 19:38
  • @JoelDuscha: That's really up to you. xparse provides a modern implementation of the macro specification (and is used with most LaTeX3 implementations). However, if you understand the LaTeX2e and TeX constructions like \@ifnextchar, then that works fine as well. – Werner Mar 18 '16 at 19:39

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.