Latex box around set of equation - latex

I would like to display a formula in latex as the following. The space between the left border and the formula should be adjustable to 1cm.
Boxed equation with 1cm space to left border
Thanks in advance!

An easy way to draw frames around aligns is the tcolorbox package:
\documentclass[fleqn]{article}
\usepackage[most]{tcolorbox}
\begin{document}
\begin{tcolorbox}[ams align,colback=white]
x &= x\\
&= x
\end{tcolorbox}
\end{document}

Related

LaTeX (amsmath): Left aligned equtations in {equation} or {align} container

I have the following problem
\usepackage[fleqn]{amsmath}
\begin{equation}
\begin{split}
\min Y\\
\textrm{s.t.}
\end{split}
\end{equation}
\begin{align}
a+b=145641574\\
c+d<e
\end{align}
creates the following output:
So far so good. The formulas are essentially left-aligned. The numbering is right-aligned. But within the formulas, splits or line breaks result in right alignment. These should also be left-aligned.
Thanks in advance and have a nice weekend!

Ignore margins and wrap text when adding pictures in Latex

I would like to add images in the top left/right or bottom left/right in a two-column page while ignoring the margins and having the text wrap around the picture. How can I also extend the solution to include half page images while ignoring the margins and with text wrapping.
I tried tikz package, but the text doesn't wrap around even when I use the wrapfig package.
For example, this is the code I used to insert image on the top left side of the page:
\documentclass[twocolumn, 12pt]{book}
\usepackage{lipsum}
\usepackage{tikz}
\begin{document}
\lipsum[1-3]
\begin{tikzpicture} [remember picture, overlay]
\node[anchor=north west,yshift=-1.5pt,xshift=1pt]%
at (current page.north west)
{\includegraphics[width=0.5\paperwidth,height=0.5\paperheight]{example.jpg}};
\end{tikzpicture}
\lipsum[1-3]
\end{document}
This results in the picture at the desired position, but the text is obscured behind the image. Thank you!
Not very pretty, but does the job I think (using geometry package only to get the correct values for top margin):
\documentclass[twocolumn, 12pt]{book}
\usepackage{lipsum}
\usepackage{tikz}
\usepackage{geometry}
\begin{document}
\begin{tikzpicture} [remember picture, overlay]
\node[anchor=north west]%
at (current page.north west)
{\includegraphics[width=0.46\paperwidth,height=0.5\paperheight]{example.png}};
\end{tikzpicture}
\vspace*{\dimexpr(0.5\paperheight-\voffset-1in-\headsep-\headheight)}
\lipsum[1-3]
\lipsum[1-3]
\end{document}

How to have a normal line of text inside a math environment and align it correctly in LaTeX?

I'm a LaTeX beginner and would like to have two equations aligned on the equals symbol which is working fine so far. Now, between these two equations I need a line of text. But LaTeX interprets this as a math formula since it's showing a (2) right next to it.
I would like to have it as normal text without a (2) aligned on the far left.
I have already tried things like
\begin{flushleft} line of text \end{flushleft}
but it's not working, maybe because I'm using it inside an {align} environment.
Not sure if it's important to mention but I'm using Overleaf (online LaTeX editor).
\begin{align}
\tilde{w}_{ij} &=
\begin{cases}
w_{ij} & \text{mit $P(i)$}\\
0 & \text{sonst}
\end{cases}\\
\text{This text be on the far left}\\
\tilde{w}_{ij} &= w_{ij} * P(i)
\end{align}
produces this ->
This sounds like a job for \intertext from amsmath:
\documentclass{article}
\usepackage{mathtools}
\begin{document}
\begin{align}
\tilde{w}_{ij} &=
\begin{cases}
w_{ij} & \text{mit $P(i)$}\\
0 & \text{sonst}
\end{cases}\\
\intertext{This text be on the far left}
\tilde{w}_{ij} &= w_{ij} * P(i)
\end{align}
\end{document}

Latex: center text within listing

How do I center text within a listing in Latex?
Or another way not using listing. (Need a box with monospace font)
The answer given at tex.stackexchange is:
\documentclass{article}
\usepackage{listings}
\renewcommand{\figurename}{Listing}
% replace figurename with the text that should preceed the caption
\begin{document}
\begin{figure}[thp] % the figure provides the caption
\centering % which should be centered
\caption{Ausgabe des C-Programms}
\begin{tabular}{c} % the tabular makes the listing as small as possible and centers it
\begin{lstlisting}[label={gtt_c_ausgabe}]
printf("Your code goes here.\n");
\end{lstlisting}
\end{tabular}
\end{figure}
\end{document}
...which still leaves me wondering:
Using a frame around the code, e.g. using \lstset{frame=single,frameround=tttt}, places the frame way over to the right. How can this be avoided?
What does the renewcommand bit do?
I don't have an answer for the listing package on top of my head, but you could try the following:
\framebox[.9\linewidth]{\parbox{.85\linewidth}{\tt Hello World\\Second line}}
That produces a box with 90% of the line width, with text of width 85% of line width.
If you want it centered you just put \centering in front of the \tt command:
\framebox[.9\linewidth]{\parbox{.85\linewidth}{\centering \tt Hello World\\Second line}}
If you prefer the box without a frame, simply change \framebox into \makebox (and keep the arguments as they stand).

Inline figures with equation-like numbering in LaTeX

For some reason I thought it would be nice to have inline figures (i.e. no floats) just like the equation environment. They would have to be numbered, as I want to be able to refer to them later on. I've come up with two attempts, but both have their shortcomings. I'm hoping for some feedback that can sort me out.
The first attempt uses 3 minipages (see the code below). This looks nice as the figure number is aligned vertically with the middle of the figure. However, as the width of the figure approaches the width of the page, things start to break down. Also, it doesn't behave very nicely at pagebreaks.
The second attempt uses the equation environment with a different label. Apart from the fact that I don't know if this is a sensible thing to do, it produces extra whitespace in the beginning of the next paragraph. It also doesn't align the label vertically in the center, but puts it on the bottom.
Here's an example of both attempts:
\documentclass{article}
\usepackage{pgf,tikz}
\usepackage{lipsum}
%
% Attempt 1
%
% Uses 3 minipages.
% Breaks if figure is wide, and at the bottom of a page.
%
\usepackage{calc}
\newlength{\figlabelwidth} % width of label
\newlength{\imgwidth} % max. width of figure
\newenvironment{inlinefig1}
{
\refstepcounter{figure} % increase figure number
\begin{center} % don't know if this is necessary
\setlength{\figlabelwidth}{\widthof{(Fig. \thefigure)}}
\setlength{\imgwidth}{\textwidth - \figlabelwidth - \figlabelwidth}
\setlength{\imgwidth}{0.9\imgwidth} % to be on the safe side
\begin{minipage}{\figlabelwidth}\makebox[\figlabelwidth]{}\end{minipage} % ghost minipage for centering
\hfill
\begin{minipage}{\imgwidth}\begin{center} % minipage for figure
}
{
\end{center}\end{minipage}
\hfill
\begin{minipage}{\figlabelwidth}(Fig. \thefigure)\end{minipage} % minipage for label
\end{center}
}
%
% Attempt 2
%
% Uses an equation environment with relabeled labels.
% Label is not centered vertically, and produces extra whitespace in the paragraph after it.
%
\def\theoldequation{\theequation} % save the old equation format
\newenvironment{inlinefig2}
{
\refstepcounter{figure} % increase figure number
\def\theequation{Fig. \arabic{figure}} % switch to figure numbering
\begin{equation}
}
{
\end{equation}
\def\theequation{\theoldequation} % reset to old equation label format
\addtocounter{equation}{-1} % correct the equation numbering
}
\begin{document}
\noindent \lipsum[1]
\begin{inlinefig1}
\begin{tikzpicture}
\draw (0,0) grid +(12,2);
\end{tikzpicture}
\end{inlinefig1}
\lipsum[2]
\begin{inlinefig2}
\begin{tikzpicture}
\draw (0,0) grid +(12,2);
\end{tikzpicture}
\end{inlinefig2}
\lipsum[3]
\end{document}
Do you guys have any better ideas, or suggestions to fix the any of the shortcomings? Thanks!
If you use the "float" package, you can specify H as a placement, which makes it appear exactly "here".
\newbox\inlinefigbox
\newenvironment{inlinefig3}
{
\refstepcounter{figure} % increase figure number
\setbox\inlinefigbox=\hbox\bgroup
}
{
\egroup
\hbox to \hsize{\hfil \box \inlinefigbox \hss (Fig. \arabic{figure})}
}

Resources