Transparent background for TikZ figures in RMarkdown/bookdown - r-markdown

I am including TikZ diagrams in my bookdown project by using code chunks, like
```{r,echo=FALSE,engine='tikz',fig.ext=if
(knitr:::is_latex_output()) 'pdf' else 'png'}
\begin{tikzpicture}
\node (A) at (0,0) {$A$};
\node (B) at (2,0) {$B$};
\draw[thick,->] (A) to (B);
\end{tikzpicture}
```
and this works fine, apart from the fact that I want the figures to have transparent backgrounds. I originally thought that my problem was coming from the conversion to png, but, having enabled caching and looking at the generated pdf files, it seems like these also have white backgrounds.
I've tried various things along the lines of engine.args=(bg='transparent'), but nothing seems to work.
How can I get transparent backgrounds? (I'd rather stick to png output if possible, but anything that is web ready is fine by me).

Related

How to improve the quality of a .png tikz output

I am using bookdown in MacOS 10.15, and I have a tikz figure which comes out great in PDF, but the PNG version (used by the HTML book) is quite ugly. For instance, some parallel lines of same length have different apparent thickness (even though they are drawn with identical parameters).
How can I improve the quality of the produced PNG?
Use chunk option fig.ext='svg' to get better picture quality

Bad quality of images using GDI+ with PostScript driver

I'm developing a program to print images of different formats (BMP, JPEG, EMF, ...) on HDC using C++ and Windows GDI+. Using the MS Publisher Imagesetter driver I can generate a postscript file and through GhostScript functions I obtain the PDF file. If I try to print the following image:
I obtain the following bad quality result with those strange squares (not present on original image):
The part of my code that I used to print the image is:
SetMapMode(hdcPrint,MM_TEXT);
Gdiplus::Graphics graphics(hdcPrint);
graphics.SetPageUnit(Gdiplus::UnitMillimeter);
Gdiplus::Image* image = Gdiplus::Image::FromFile(srPicture->swPathImage);
graphics.DrawImage(image,x,y,w,h);
I tried to print the same image with many drivers and different kind of formats (different from PostScript: PDF, EMF, real printer) and the result is always acceptable (the squares are not present).
Furthermore, I tried to open the bad quality result with a pdf reader different from Adobe Acrobat Reader DC (Wondershare PDFelement and Chrome) and, even then, the result is acceptable.
I also noticed that if the image contains some different shapes (i.e. a big red line, like in the next image) the result is good too.
At this point, I have no idea if the problem is Adobe reader or my implementation.
Is there a differnt way to print different formats images with GDI+ (or pure GDI)?
The PostScript file generated is this.
Well... You haven't supplied either the PostScript or PDF files, which makes it really hard to comment.
Its not completely obvious to me at what point you are getting the image you show, is this what you see on the PDF file ? Is it something you are getting when printing the PDF file to a physical printer ? If its the lattter, how are you printing the PDF file to the printer ?
The JPEG you have supplied a link to is really small (6Kb), are you genuinely trying to use that JPEG file ?
My guess (and in the absence of any files, a guess is all it can be) that you are using an old version of Ghostscript. Old versions would decompress the JPEG image, then recompress the image using whatever filter produced the smallest result, usually JPEG again.
Because JPEG is a lossy format, every time you apply it to an image the quality decreases.
Newer versions of Ghostscript don't decompress the JPEG image data when going to the pdfwrite device, unless other optsions (eg Colour conversion, image downsampling etc) make it neccesary. The current version of Ghostscript is 9.27 and the release of 9.28 is imminent, I'd suggest you try one of those.
Another possibility would be that either the PostScript program has been created in such a way as to degenerate every image smaple to a rectangle, or you are using an extremely old version of Ghostscript where that technique was also used.
Note that none of these would, in my opinion, lead to exactly the result you've pasted here, but the version is certainly worth investigating. Posting the PostScript program file (ie the file you send to Ghostscript) would be more helpful, because it would allow me to at least narrow down where the problem has occured.
[EDIT]
The fault appears to be an intriguing bug in Acrobat.
The PostScript program uses a colour transfer function to invert the colour samples of the RGB JPEG image. (this is a frowned upon practice, its not what transfer functions are for, but its not uncommon). Ghostscript's pdfwrite device preserves the transfer function.
When rendered Ghostscript correctly produces the expected result, Acrobat, however, spectacularly does not, I have no idea what kind of mess they've made which leads to the result you get but its clearly wrong.
If I alter Ghostscript's pdfwrite production settings to Apply transfer functions instead of preserving them:
-c "<</TransferFunctionInfo /Apply>> setdistillerparams" -f PostScript.ps
then the resulting file views correctly in Acrobat. If I modify Adobe Acorbat's settings so that it uses Preserve instead of Apply for transfer functions (look in Settings->Edit Adobe PDF Settings, then the Color tab, and at 'when transfer functions are found', set the drop-down to Preserve instead of Apply) the resulting PDF file renders correctly in Ghostscript, and the same kind of incorrectly in Acrobat as the Ghostscript pdfwrite output file.
In short I'm afraid what you are seeing here is an Acrobat rendering bug, you can work around it by altering the Ghostscript transfer function settings as above but its really not a problem in Ghostscript.

How to keep font size constant in Inkscape during rescaling?

I came across a problem in Inkscape I couldn't solve on my own. Currently I am creating sketches etc. for a LaTex document in Inkscape. Drawing the sketches works fine and adding text to it as well. Then I would just save it as pdf (+ this tex option) and then import it in LaTex using
\begin{figure}
\centering
\input{testfile.pdf_tex}
\label{fig:testfile}
\caption{This is just a testfile}
\end{figure}
The font size and type in the sketch after compiling the document remains always the same as in the rest of my LaTex document.
However, placing the text in the right position requires a lot of iterative steps, because rescaling the sketch in Inkscape (I might find the sketch too big or too small for example) would rescale the text as well - again, just in Inkscape, luckily not in LaTex.
How do I prevent the text in Inkscape to be rescaled? Everything that should change during rescaling is the absolute position of the text boxes while the relative position and font size should remain constant.
I had the same problem and found this Inkscape extension Scientific-Inkscape to be very useful.
I cite one of its features "Scale Plots: Changes the size or aspect ratio of a plot without modifying its text and ticks. Especially useful for assembling multi-panel figures."
See also a scaling example here:

How do I render high quality monochrome text with SDL2_TTF?

I'm writing a game with old school low resolution graphics. I'm using SDL2_TTF to render monochrome text but it doesn't seem to be hinted properly. I set the hinting with TTF_SetFontHinting(font, TTF_HINTING_MONO) and rendered the text with all three rendering functions (TTF_RenderText_Solid, TTF_RenderText_Shaded and TTF_RenderText_Blended) but I'm not happy with the results. Here's the output I'm getting from TTF_RenderText_Solid. The other functions output antialiased text.
And here's the output I want. This is Andale Mono 12pt (same as above) rendered by the Mac Terminal emulator and scaled up 4x.
The quality of the output does matter because the text is scaled up 4x when shown to the player. Why are they different? I thought that FreeType was the de-facto font rendering library so SDL2_TTF and Terminal would both be using it.
Am I simply using the SDL2_TTF library incorrectly?
I didn't want to mess with the SDL2_TTF library to achieve the effect I was looking for so I decided to find a different font that looks better. I found that if I chose a font that looks pixelated and render it at just the right point size everything looks fine.

Optimized .plist/textures for walk animations

I am trying to 'squeeze' my textures for walking animations. The anim has 8 frames, but actually can be done quite well with 1-2-3-4-5-4-3-2 which would fit nicely in a 128x128 points texture. Do you know of a tool that can create the plist entries for 6-7-8 that are mapped onto the 4-3-2 areas of the texture ?
Coding is still an option, but was wondering if some tool has that out of the box.
I'm surprised there's still Cocos2D developers out there who aren't using TexturePacker. :)
Check out the Alias Creation section under Features, I'm quoting (but also can confirm that this works perfectly):
If two images are identical after trimming only one image is placed in
the sprite sheet. The duplicates will just be added to the description
file allowing you to access it with both names.
This is perfect when using animations: You simply don't have to care about equal phases.