0

I would like to learn how auto-program my document to display integrals with fractions nicely.

My MWE:

\documentclass{article}
\usepackage{amsmath}
\begin{document}

$\int_{\frac{1}{2}}^{\frac{\pi}{10}} \frac{x^2+x+1}{e^x +3}$ \vspace{1cm} %standard display

$\frac{x^2+x+1}{e^x+\frac{3}{2}}$ \vspace{1cm} %standard display 

\end{document}

What I would like to know is how do I program it so that if you have a limits with fractions use \tfrac for the limits but if you have a fraction on it own, use \dfrac.

Desired result:

\documentclass{article}
\usepackage{amsmath}
\begin{document}

$\displaystyle\int_{\tfrac{1}{2}}^{\tfrac{\pi}{10}} \dfrac{x^2+x+1}{e^x +3}$ \vspace{1cm} %uses \tfrac for limits and \dfrac for fraction

$\dfrac{x^2+x+1}{e^x+\frac{3}{2}}$ \vspace{1cm}  %uses \dfrac because its a fraction 

\end{document}
6
  • Is this truly inline mathematics? If you're using display math (via \[ and \] (or the non-advised $$)), then this would happen automatically.
    – Teepeemm
    Oct 27, 2020 at 1:10
  • I wish to force the document to auto-toggle between the two depending on the expression. Like I said, if i can make a command say ``` \dtfrac{}{} ``` as a general expression for fraction, then say I use it in an integral with limits, then it would use the "\tfrac{}{}" version, else it would just use the "\dfrac{}{}" version.
    – Alan Jones
    Oct 27, 2020 at 1:36
  • why can you not simply use \frac ? and use display math environments such as \[ for the displays then you will not need $\displaystyle Oct 27, 2020 at 9:37
  • What we're trying to say is that the auto-toggle you're looking for is automatic if you use the display environments \[, equation, etc.
    – Teepeemm
    Oct 27, 2020 at 12:42
  • But using [ places the equation right in the middle. It is the same as $$.
    – Alan Jones
    Oct 27, 2020 at 22:27

2 Answers 2

1

If you use mathematics environments (e.g. \begin{equation} ... \end{equation} from amsmath then most of the maths elements will be in display style but (as you may have noticed) the fractions in your integral limits will still be small (text-style).

The most straightforward approach I can think of is to define a custom command that has the integral `template' how you would like.

To achieve your integral with fractions in both limits, you could use the command (I've named it \intfrac) as \newcommand{\intfrac}[5]{\displaystyle\int_{\tfrac{#1}{#2}}^{\tfrac{#3}{#4}} #5 } which you would use (as per your MWE) as \intfrac{1}{2}{\pi}{10}{\frac{x^2+x+1}{e^x +3}} to achieve what you wish. This works both in-line and in a maths environment.

A downside is that this requires you to make a command for every 'instance' of fraction limits - like when only one limit is a fraction. So it's not the best solution, but it may work for you.

\documentclass{article}
\usepackage{amsmath}

\newcommand{\intfrac}[5]{\displaystyle\int_{\tfrac{#1}{#2}}^{\tfrac{#3}{#4}} #5 }

\begin{document}

% within environment. Notice typical answer has small fractions.
\begin{equation}
\intfrac{1}{2}{\pi}{10}{\frac{x^2+x+1}{e^x +3}}\\
\int_{\frac{1}{2}}^{\frac{\pi}{10}} \frac{x^2+x+1}{e^x +3}
\end{equation}

% in-line environment
$\intfrac{1}{2}{\pi}{10}{\frac{x^2+x+1}{e^x +3}}$

% from your MWE
$\displaystyle\int_{\tfrac{1}{2}}^{\tfrac{\pi}{10}} \frac{x^2+x+1}{e^x +3}$ \vspace{1cm} %uses \tfrac for limits and \dfrac for fraction

$\dfrac{x^2+x+1}{e^x+\frac{3}{2}}$ \vspace{1cm}  %uses \dfrac because its a fraction 

\end{document}

enter image description here

1
  • I wish to automatically toggle between the two. For example, this approach will still force me to use \intfrac (where ever i have integrals with fractions as limits) and \dfrac where its a normal fraction outside the integral limits. Ideally I would like to use say \dtfrac{}{} and it automatically interprets that hey if its used in an integral, use the \tfrac else use \dfrac.
    – Alan Jones
    Oct 27, 2020 at 1:34
1

As far as I understand, the switch that you ask for is the standard behaviour of \frac using an inline (script) style in the limits and display style in the top level of display math

enter image description here

\documentclass{article}
\usepackage{amsmath}
\begin{document}

\[\int_{\frac{1}{2}}^{\frac{\pi}{10}} \frac{x^2+x+1}{e^x +3}\]



\[\frac{x^2+x+1}{e^x+\frac{3}{2}}\]

\end{document}
3
  • But using [ places the equation right in the middle of the page. It is the same as $$. I want it to go with the flow of the expression.
    – Alan Jones
    Oct 27, 2020 at 22:28
  • @AlanJones \[ is not the same as $$ one is a supported latex construct and the other is not. In inline math ($ or \( ) then naturally textstyle fractions are used as inline style aims to preserve the baseline spacing of the surrounding paragraph. Oct 27, 2020 at 22:50
  • @AlanJones your examples only had the math expression on the line so display math is more appropriate than inline, Use [fleqn] document class option if you want equations flush left not centred. Oct 27, 2020 at 22:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .