Each Optifine version is stated(by Optifine, not by Forge) to be compatible with a specific version (build) of Forge, they denote the compatible version in each version's changelog.
i.e. https://optifine.net/changelog?f=OptiFine_1.12.2_HD_U_D3.jar
has this line:
Note that the installer will not work for Twitch launcher because it handles launching the game differently, to change the Forge version used by a Twitch profile click the following buttons:
... (ellipses)
Profile Options
Untick the "Locked" box (Pre-made packs only, note the warning Twitch gives you to the right of this box)
Select the version from the dropdown that is specified by the Optifine version you are using
Personally I tend to get any packs straight from the CurseForge site and import them into MultiMC, as I feel it gives me more control with a cleaner interface.