knitr not aligning figures to center when image is expanded (.pdf document) - latex

I am trying to increase the size of an image in a .pdf document using knitr, but when I try to do this the image shifts to the right and off the page. Here is the syntax I am using:
```{r test_image, echo = FALSE, out.width = '150%', fig.align = 'center'}
knitr::include_graphics("test_image.png")
```
I have also tried inserting fig.height and fig.width to adjust the height (instead of using out.width) but I still get the same thing. I have also tried fig.align = 'left' to keep the image on the page but that does not solve the issue either.
Has anyone else noticed this behavior? Is there a way to maintain the image centering when increasing the image size?

My thoughts.
I think, that this problem exists because of "big amount" of whitespace on left and right sides of your picture.
You can trim it manually and add after with knitr::include_graphics or do it with latex straightaway:
Add to your header:
header-includes:
- \usepackage{graphicx}
Add your pic
\includegraphics [trim={5cm 0 5cm 0},clip]{Ml6KL.png}
#you can also customize your width/height
#\includegraphics [trim={5cm 0 5cm 0},clip, width = XX, height = XX]{Ml6KL.png}
An output:
P.S. If you can - try to find better versions of first two logos(or scale < 1). As you can see - the quality leaves much to be desired...

Related

How to align two UILabels on the Y axis of the center of the first line of text

please see the image below for two examples of what is to be achived
the alignment should be on the Center Y of the first lines of each UILabels and should work regardless of font size or font. currently we have implemented this with different constraints to the top of the super view for different font and font size combinations.
the constraint to align the center of the two UILabels does not work since the text of the second UILabel is not fixed and can have several lines.
also the text is dynamic, so it is not known where the text will wrap to create the first line, thus it cannot be shown in an one line UILabel with the rest of the text in another one below.
currently this is implemented using UIKit, but if there is an easy solution in SwiftUI we can put these two labels in a SwiftUI component. so a SwiftUI solution would also be welcomed.
Your comments said "it should be on the glyphs" ... but, without additional information, my guess is that "real world" usage would not really need that level of precision.
For example:
While the glyphs are not perfectly center-Y aligned, it seems unlikely you'd run into a case where the first line of the "rightLabel" is " ' " ' " or . , . , ..
This layout can be easily done with only a few constraints - no need to do any calculations:
The "Positioning" label would, of course, be set .hidden = true so it would never be seen.
If you really, really want glyph-precision, you'll need to calculate
the Glyph bounding box for the left-label
the Glyph bounding box for first line of the right-label
calculate the "character box" offsets to align the Glyph Y-centers
and then position the two labels accordingly, or use Core Text to draw the text (instead of using UILabel).
Probably more work than necessary -- unless your actual use-case demands it.
That's an interesting problem! You can try using the centerYAnchor for the label on the left, and the firstBaselineAnchor for the label on the right... that will align the center Y with the text baseline, which isn't quite what you want.
To find the correct offset to apply, you can use the information from UIFont about the size of the characters. I'd probably start with capHeight * 0.5 and see if that looks or feels right. Something like:
leftLabel.centerYAnchor.constraint(equalTo: rightLabel.firstBaseLineAnchor, constant: rightFont.capHeight * 0.5)
This is a more difficult problem in SwiftUI, I think, because resolved font metrics aren't directly available to you.

Alignment in flextable correct in Rstudio viewer, incorrect in Word

I am trying to left align some columns in a few of my tables. Most of the time this works no problem, but other times, the columns selected with:
ft <-align(ft, j=1:2, align = "left", part = "body")
ft <-align(ft, j=4:6, align = "left", part = "body")
look correct in the Rstudio viewer. I used a loop to make 39 tables and want to add them to a word document using officer
However, when printed to a word doc using:
doc <- read_docx() %>%
body_add_flextable(tab0)
body_add_break(doc, pos="after")
for (i in 1:38) {
body_add_flextable(doc,get(paste0("tab", i)), align = "left")
body_add_break(doc, pos="after")
}
print(doc,target=documentname)
it center aligns the columns (only changes those that were left aligned). When I click on the text, it says that they are left aligned, but then I center and re-left align and that fixes the issue. I have only noticed the issue which character variables
I tried triming whitespace with
trimws() before I make the flextable with no luck.
I tried other alignments and the issue only seems to occur with left alignment.
I don't believe my code to be wrong, since it works for my other tables. Therefore, I think trying to make reproducible code would be a waste of time.
Therefore, I'm assuming the text itself is causing the issue? Does anyone know what could be wrong with the character variables themselves? Or any other ideas?
Fixed it...
Somewhere within my flextable manipulation this leading white space was added.
So trimming white space before didn't do anything. Had to trim right before naming my table (within a loop). Here is the loop I used to correct it.
for (j in 1: length(ft[["body"]][["content"]][["content"]][["data"]])){
ft[["body"]][["content"]][["content"]][["data"]][[j]][["txt"]] <- trimws(ft[["body"]][["content"]][["content"]][["data"]][[j]][["txt"]])
}

How insert some space before and after images in Sphinx

I created a document via Sphinx with the make latexpdf command.
Now I have the problem that LaTeX is setting my picture at the top directly after the text and at the bottom of the image it puts 3 or 4 linebreaks.
Is there a possibility to get 1 space at the top and 1 space at the bottom of the picture without
having to use a \ in the syntax?
My code:
title:
===========
some text
.. image:: screenshots/manage_products.png
some more text
another title:
===========
EDIT: Here a little Screenshot to show you my problem. I also looked into the main documentation of Sphinx (+ Syntax documentation) and found no answer for this problem!
Btw: Ignore the red arrow plz.
You could use the .. figure:: directive to add the extra spacing you would like around your image.
title:
===========
some text
.. figure:: screenshots/manage_products.png
some more text
I like using the figure directive more as well, since you have some additional control elements to your image, such as labeling.
.. figure::
Your figure text here
Wanted to add a narrow line after the image and tried all sorts of combinations and all either did not work or gave a wide line after the image.
Ended up creating a tiny empty.png image and inserted into the page with the following:
.. image:: _static/main.png
:align: center
:scale: 70 %
.. image:: _static/empty.png
.. empty.png image above to add space after main.png image

Caption in longtable

I find that the caption in my longtable takes two lines but it seems that it can fit in one line. See the figure below:
My code is:
\begin{longtable}{|c|c|c|c||c|c|c|}
\caption{Testing error for training size 100000 and 8000 random counts}\\
\hline
\multicolumn{2}{|c|}{Concept} & \multicolumn{2}{c||}{Negative Class} & \multicolumn{2}{c|}{Positive Class} & Error rate \\
\hline
...
\end{longtable}
How to make the caption fit into a single line?
EDIT:
Thank, Geoff. But I tried "\usepackage{fullpage}" as you suggested, the whole content in the pdf file is messed up.
As you can see, there is still lot of space on the left of the caption. If the caption can be moved to the left, it will fit into a single line.
You can do this:
\LTcapwidth=\textwidth
\begin{longtable}
Two options:
The longtables documentation says that there is an LTcapwidth variable that you can set, that defaults to 4in. Try \setlength{LTcapwidth}{5.2in}. I'm not sure how this works if you just want to change one caption width, so
You could force an hbox in the caption of the right dimensions:
\caption{\hskip -0.7in \hbox to 5.2in{Testing error for training size 100000 and 8000 random counts}}
one way to force writing on a single line is to include the title of your caption within mbox as in
\mbox{This is a very long title}
You can try
\caption{\mbox{This is a very long title}}
or
\mbox{\caption{This is a very long title}}
My guess is that your table is wider than the page. In other words, your caption is as wide as the margins allow, while your table is simply too wide.
Try putting \usepackage{fullpage} in your preamble.
Just a guess, but try:
\usepackage[margin=1cm]{caption}
This is basically the same as xl42ii's answer, but works correctly in landscape mode. Use \linewidth instead of \textwidth. The reason for this is that the landscape mode swiches the values of \textwidth and \textheight, while the value of \linewidth stays the same.
So
\LTcapwidth=\linewidth
\begin{longtable}
will do the trick. It did for me. : )
Just add a multiplier (like 1.2 for example) to the setting, this will solve all your landscape issues:
\LTcapwidth=1.2\textwidth
\begin{longtable}

The way to find out whole picture width in PGF (Latex)

I've got a latex macro that draws a picture using PGF and Tikz according to given parameters. The width of picture drawn depends on these parameters.
PGF automatically calculates the resulting width of any picture drawn so the user does not have to set it explicitly(like for example when using latex build in picture environment).
However I need to know the width of picture that will be drawn. Of cause I could calculate it as the PGF does but this is going to be quite some work(a lot of if statements...). Is there a way to ask PGF what is the width of picture that is to be drawn (some command I expect)? Either inside tikzpicture environment or just after it?
Thanks for help.
What I would probably do is put the tikzpicture environment in a box, and then find the width of the box:
\setbox0=\vbox{\hbox{%
\begin{tikzpicture}
% ...
\end{tizpicture}%
}}
The width of the following picture is {\the\wd0}.
\box0
Note that after you run \box0, the box will be inserted into the document and its contents will be destroyed; you thus need to query the width of the box before you do that. If you want to save the width, you can store it in a dimension register with \dimen0=\wd0; alternatively, you can use \copybox0, which inserts the box but doesn't destroy it (although this might leak memory).
Also, having played with some of this before, I found that using just a \vbox caused the box to always be the full width of the page (which, if you think about it, makes sense); however, using just an \hbox caused the unboxing to fail for some reason (it seemed to be a known pug). Using both like this works, however—I'm using something very much like this to render TikZ pictures to PDF files of precisely the right size.

Resources