Repository: hannah-sten/texify-idea
Issue: Compiling using Latexmk produces DVI output no matter what dropdown option is set in the output format.

#### Type of JetBrains IDE (IntelliJ, PyCharm, etc.) and version
PyCharm 2020.2

#### Operating System 
Windows 10

#### TeXiFy IDEA version
0.7.1-alpha.3

#### What I did (steps to reproduce)
Compiled a .tex file using Latexmk with the following `.latexmkrc` options:

```
$latex = 'latex  %O  --shell-escape %S';
$pdflatex = 'lualatex  %O  --shell-escape %S';
```

and "PDF" selected in the output format dropdown.

#### Expected behavior
A .PDF file is produced.

#### Actual behavior

A .DVI file is produced (which often results in compilation errors such as "Cannot determine size of graphic" for `\\includegraphics`)

#### Temporary Fix

Adding `-pdf` to the Custom compiler arguments fixes the problem.


Changed files:
src/nl/hannahsten/texifyidea/util/Magic.kt, src/nl/hannahsten/texifyidea/util/LatexmkRcFileFinder.kt, src/nl/hannahsten/texifyidea/run/compiler/LatexCompiler.kt, src/nl/hannahsten/texifyidea/run/latex/LatexRunConfiguration.kt, src/nl/hannahsten/texifyidea/inspections/latex/LatexMissingLabelInspection.kt

diff --git a/src/nl/hannahsten/texifyidea/inspections/latex/LatexMissingLabelInspection.kt b/src/nl/hannahsten/texifyidea/inspections/latex/LatexMissingLabelInspection.kt
index 197349a3e..da3162851 100644
--- a/src/nl/hannahsten/texifyidea/inspections/latex/LatexMissingLabelInspection.kt
+++ b/src/nl/hannahsten/texifyidea/inspections/latex/LatexMissingLabelInspection.kt
@@ -25,7 +25,7 @@ import org.jetbrains.annotations.Nls
 import java.util.EnumSet
 
 /**
- * Currently only works for Chapters, Sections and Subsections.
+ * Check for commands which should have a label but don't.
  *
  * @author Hannah Schellekens
  */
diff --git a/src/nl/hannahsten/texifyidea/run/compiler/LatexCompiler.kt b/src/nl/hannahsten/texifyidea/run/compiler/LatexCompiler.kt
index 57fbb3f0a..c11572f9a 100644
--- a/src/nl/hannahsten/texifyidea/run/compiler/LatexCompiler.kt
+++ b/src/nl/hannahsten/texifyidea/run/compiler/LatexCompiler.kt
@@ -82,6 +82,8 @@ enum class LatexCompiler(private val displayName: String, val executableName: St
 
         override val handlesNumberOfCompiles = true
 
+        override val outputFormats = arrayOf(Format.DEFAULT, Format.PDF, Format.DVI)
+
         override fun createCommand(
             runConfig: LatexRunConfiguration,
             auxilPath: String?,
@@ -91,7 +93,7 @@ enum class LatexCompiler(private val displayName: String, val executableName: St
         ): MutableList<String> {
             val command = mutableListOf(runConfig.compilerPath ?: "latexmk")
 
-            val isLatexmkRcFilePresent = LatexmkRcFileFinder.isLatexmkRcFilePresent(runConfig.compilerArguments, runConfig.mainFile?.parent?.path)
+            val isLatexmkRcFilePresent = LatexmkRcFileFinder.isLatexmkRcFilePresent(runConfig)
 
             // If it is present, assume that it will handle everything (command line options would overwrite latexmkrc options)
             if (!isLatexmkRcFilePresent) {
@@ -100,6 +102,9 @@ enum class LatexCompiler(private val displayName: String, val executableName: St
                 command.add("-file-line-error")
                 command.add("-interaction=nonstopmode")
                 command.add("-synctex=1")
+            }
+
+            if (runConfig.outputFormat != Format.DEFAULT) {
                 command.add("-output-format=${runConfig.outputFormat.name.toLowerCase()}")
             }
 
@@ -377,6 +382,7 @@ enum class LatexCompiler(private val displayName: String, val executableName: St
      */
     enum class Format {
 
+        DEFAULT, // Means: don't overwite the default, e.g. a default from the latexmkrc, i.e. don't add any command line parameters
         PDF,
         DVI,
         HTML,
diff --git a/src/nl/hannahsten/texifyidea/run/latex/LatexRunConfiguration.kt b/src/nl/hannahsten/texifyidea/run/latex/LatexRunConfiguration.kt
index 2845ced91..d9b125267 100644
--- a/src/nl/hannahsten/texifyidea/run/latex/LatexRunConfiguration.kt
+++ b/src/nl/hannahsten/texifyidea/run/latex/LatexRunConfiguration.kt
@@ -487,7 +487,7 @@ class LatexRunConfiguration constructor(project: Project,
     override fun getOutputFilePath(): String {
         val outputDir = outputPath.getAndCreatePath()
         return "${outputDir?.path}/" + mainFile!!
-                .nameWithoutExtension + "." + outputFormat.toString()
+                .nameWithoutExtension + "." + if (outputFormat == Format.DEFAULT) "pdf" else outputFormat.toString()
                 .toLowerCase()
     }
 
diff --git a/src/nl/hannahsten/texifyidea/util/LatexmkRcFileFinder.kt b/src/nl/hannahsten/texifyidea/util/LatexmkRcFileFinder.kt
index c6cca9098..49d3af008 100644
--- a/src/nl/hannahsten/texifyidea/util/LatexmkRcFileFinder.kt
+++ b/src/nl/hannahsten/texifyidea/util/LatexmkRcFileFinder.kt
@@ -2,6 +2,8 @@ package nl.hannahsten.texifyidea.util
 
 import com.intellij.openapi.util.SystemInfo
 import com.intellij.openapi.vfs.LocalFileSystem
+import nl.hannahsten.texifyidea.run.compiler.LatexCompiler
+import nl.hannahsten.texifyidea.run.latex.LatexRunConfiguration
 import java.io.File
 
 /**
@@ -69,7 +71,13 @@ object LatexmkRcFileFinder {
         return false
     }
 
-    fun isLatexmkRcFilePresent(compilerArguments: String?, workingDir: String?): Boolean {
-        return isSystemLatexmkRcFilePresent || isLocalLatexmkRcFilePresent(compilerArguments, workingDir)
+    fun isLatexmkRcFilePresent(runConfig: LatexRunConfiguration): Boolean {
+        val isPresent = isSystemLatexmkRcFilePresent || isLocalLatexmkRcFilePresent(runConfig.compilerArguments, runConfig.mainFile?.parent?.path)
+
+        // The first time, by default don't override what's in the latexmkrc (but avoid resetting the user chosen output format)
+        if (isPresent && !runConfig.hasBeenRun) {
+            runConfig.outputFormat = LatexCompiler.Format.DEFAULT
+        }
+        return isPresent
     }
 }
\\ No newline at end of file
diff --git a/src/nl/hannahsten/texifyidea/util/Magic.kt b/src/nl/hannahsten/texifyidea/util/Magic.kt
index 9cc2894d3..7b5c14a32 100644
--- a/src/nl/hannahsten/texifyidea/util/Magic.kt
+++ b/src/nl/hannahsten/texifyidea/util/Magic.kt
@@ -235,7 +235,7 @@ object Magic {
         val figures = hashSetOf("figure")
 
         @JvmField
-        val verbatim = hashSetOf("verbatim", "Verbatim", "lstlisting", "plantuml", "luacode", "luacode*")
+        val verbatim = hashSetOf("verbatim", "Verbatim", "lstlisting", "plantuml", "luacode", "luacode*", "sagesilent", "sageblock", "sagecommandline", "sageverbatim", "sageexample")
 
         @JvmField
         val algorithmEnvironments = setOf("algorithmic")
@@ -249,7 +249,7 @@ object Magic {
                 "matrix*", "pmatrix*", "bmatrix*", "vmatrix*", "Bmatrix*", "Vmatrix*",
                 "smallmatrix", "psmallmatrix", "bsmallmatrix", "vsmallmatrix", "Bsmallmatrix", "Vsmallmatrix",
                 "smallmatrix*", "psmallmatrix*", "bsmallmatrix*", "vsmallmatrix*", "Bsmallmatrix*", "Vsmallmatrix*",
-                "gmatrix"
+                "gmatrix", "tikz-cd"
         )
 
         @JvmField