2

I have a litte problem with using the \frac command, because the text that is written before it, it will be spaced. How can i fix this? It is on purpuse that there is two lines of text one under the other. It can work if i dont have the \displaystyle in my equation (the second equation) but then it just looks small.

\documentclass[11pt]{article}

\usepackage{fontspec}
\usepackage{unicode-math}
\usepackage{amsmath}
\usepackage{mathptmx}
\usepackage{fancybox}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{pgfplots}
\pgfplotsset{compat = newest}
\usepackage{array}
\usepackage{delarray}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{decorations.markings}
\usepackage{caption}
\usepackage{subcaption}
\usepackage[utf8]{inputenc}
\usepackage[danish]{babel}
\usepackage{booktabs}
\usepackage[dvipsnames]{xcolor}
\usepackage[paperheight=11.693in,paperwidth=8.267in]{geometry}

\setmainfont{Times New Roman}
\setmathfont{Times New Roman}

\geometry{left=3.4cm}
\pagestyle{empty}

\begin{document}

    \text{\textit{x} og \textit{y} er ligefrem proportionale} \hspace{1cm} \text{(8)} \hspace{0.5cm} $\textit{y}= \textit{k}\cdot\textit{x}$ \hspace{2cm} $\displaystyle \frac{\textit{y}}{\textit{x}}= \textit{k}$\\ 

\text{Proportionalitetsfaktor \textit{k}}

\end{document}
19
  • 1
    welcome! could you make your snippet into a complete example we can compile? Commented Dec 12, 2024 at 19:09
  • im totally new to this. What do you mean @cfr Commented Dec 12, 2024 at 19:16
  • @olivers82 you just include a code that we can copy and paste to run in LaTeX. It would be something like \documentclass{article} \usepackage{mathtools} \begin{document} [your code here] \end{document}. Commented Dec 12, 2024 at 19:20
  • 3
    first, you cannot use all those packages because some are simply incompatible so only one of them can 'win'. second, it doesn't matter what is needed for the other 19 pages. all that matters is what is needed to reproduce the problem. we're not saying: delete all those packages in your real document. we're saying: delete the ones we don't need to reproduce from your minimal example. Commented Dec 12, 2024 at 19:42
  • 1
    @olivers82 - you wrote, "Ohh i also dont use \begin{equation} for the numbered equations because i want it to appear before the equation and not after." If you want equation numbers to be placed on the far left, just specify leqno as one of the \documentclass options. Commented Dec 12, 2024 at 19:52

3 Answers 3

5

The entire reason for having separate text and display styles for math is so that the inline text style layout does not disturb the inter-line spacing in paragraphs. So the correct markup is

abc ... $\frac{y}{x}=k$

not

abc ... $\displaystyle \frac{y}{x}=k$

By using display style in an inline context you are explicitly telling TeX to break the even interline space.

If you need display style, then you should use a displayed equation, so

abc ...
\[\frac{y}{x}=k\]

Note that in either case you should not ever need an explicit \displaystyle command in a document, it is used to define display environments such as align that use displaystyle while internally using inline math in table cells.

2

Maybe something like this?

enter image description here

\documentclass[11pt]{article}
\usepackage[danish]{babel}
\usepackage[a4paper,left=3.4cm]{geometry}
\usepackage{unicode-math} % 'unicode-math' loads 'fontspec' and 'amsmath' automatically

\setmainfont{Times New Roman}
\setmathfont{XITS Math}[Scale=MatchLowercase] % or some other suitable math font

\begin{document}

$x$ og $y$ er ligefrem proportionale 
\hspace{1.0cm} (8) % what is '(8)' supposed to mean?
\hspace{0.5cm} $y= k\cdot x$ 
\hspace{2.0cm} $\smash{\dfrac{y}{x}}= k$

Proportionalitetsfaktor $k$
\end{document}
7
  • OMG! Thank you so Much! That looks just like i wanted it. Btw the (8) is the number of that equation Commented Dec 12, 2024 at 20:01
  • @olivers82 never hard-code labels of equations or anything else. it breaks cross-referencing and, if used, it will break hyperlinks, lists of things etc. it is also a complete pain to maintain as an author. if you insert an equation near the beginning, you have to renumber everything after. Commented Dec 12, 2024 at 20:03
  • So one last thing and i wont bother anymore. Fx this number (8). if i wanted it just like you just did there. That exact location before an equation. is there a way so everytime i make a new equation they will be numbered automaticly and they all have to be the same distance to the equation (like you did there)? Commented Dec 12, 2024 at 20:07
  • @olivers82 it gets automatically numbered if you use the equation or align environments. That's why I recommend using them. If you want to make a number for some equation outside I imagine it is more involved. Commented Dec 12, 2024 at 20:19
  • 1
    @olivers82 - I suggest you change \documentclass[11pt]{article} to \documentclass[11pt,leqno,fleqn]{article}. That way, (a) displayed equations' number will be typeset at the far left and (b) the equations themselves will be placed on the left rather than in the center. And, for goodness' sake, do make an effort to inform yourself about TeX's two math modes: inline math and display math. Commented Dec 12, 2024 at 20:52
2

So, one thing you can do is measure the difference between the height of $\frac{a}{b}$ and $\displaystyle \frac{a}{b}$ in a variable, call it \fracHeightDiff. You can then use \vspace{-\fracHeightDiff} to move the text up by the difference. Thus, it will be as if you didn't use displaymode.

\documentclass[a4paper]{article}
\usepackage{mathtools}
\usepackage{calc}

% Measure height of displayed fraction
\newlength{\fracDisplay}
\settoheight{\fracDisplay}{$\displaystyle \frac{a}{b}$}

% Measure height of inline fraction
\newlength{\fracInline}
\settoheight{\fracInline}{$\frac{a}{b}$}

% Find the difference
\newlength{\fracHeightDiff}
\setlength{\fracHeightDiff}{\fracDisplay-\fracInline}

\begin{document}

\noindent $x$ og $y$ er ligefrem proportionale \hspace{1cm} $(8)$ \hspace{0.5cm} $y=k\cdot x$ \hspace{2cm} $\displaystyle \frac{y}{x}=k$ 
\vspace{-1\fracHeightDiff} % Using this to get negative vertical spacing
\newline
Proportionalitetsfaktor $k$.

\vspace{2cm}
\noindent Proportionalitetsfaktor $\displaystyle \frac{a}{b}$ 
\vspace{-1\fracHeightDiff} \newline
Proportionalitetsfaktor $\ \frac{a}{b}$ \\
Propo \\
Propo
\end{document}

However: a word of caution: the fraction will then overlap with the next line. The reason for this spacing is simply so that the text do not overlap. If you restore the original spacing, you run the risk of overlapping text. As shown in the example with a shorter line of text:

enter image description here

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.